The country_code in this case can be DE or GB.
var cc = country_code
if (cc.equals == "GB"){
console.log("You're in the UK")
}
else {
console.log("You're not in the UK")
}
Why is this statement throwing up incorrect responses?
EDIT:
The missing " was a typo. The solutions given haven't work so far.
I set country_code to be the text response of a XMLHttpRequest object. If that helps?
The country_code in this case can be DE or GB.
var cc = country_code
if (cc.equals == "GB"){
console.log("You're in the UK")
}
else {
console.log("You're not in the UK")
}
Why is this statement throwing up incorrect responses?
EDIT:
The missing " was a typo. The solutions given haven't work so far.
I set country_code to be the text response of a XMLHttpRequest object. If that helps?
Share Improve this question edited Jan 11, 2016 at 4:59 Henry asked Jan 11, 2016 at 4:22 HenryHenry 31 silver badge4 bronze badges 9-
5
Don't need
.equals
, usecc === "GB"
.console.log(You're not in the UK")
Missing start quote,"
. – Tushar Commented Jan 11, 2016 at 4:22 - is country code is object or you are paring wrong ? I highly doubt in this line if @Tushar ment is just a typo – Anik Islam Abhi Commented Jan 11, 2016 at 4:24
- Possible duplicate of What is the correct way to check for string equality in JavaScript? – Tushar Commented Jan 11, 2016 at 4:24
- @Tushar that should be an answer. – elixenide Commented Jan 11, 2016 at 4:24
- @EdCottrell What about dupe – Tushar Commented Jan 11, 2016 at 4:25
4 Answers
Reset to default 2You must use the Equal (==
) operator to check if the value of cc
is equal to the string literal "GB"
. Or you can use the Strict equal (===
) operator to see if the operands are equal and of the same type.
Also the argument to the console.log
call in the else
branch must be within quotation marks, or else you'll get syntax error. String literals must always be enclosed in '
or "
.
var cc = country_code;
if (cc == "GB"){
console.log("You're in the UK")
}
else {
console.log("You're not in the UK")
}
The country_code in this case can be DE or GB.
That means the variable country_code
is a string.
So, cc.equals == "GB"
will return undefined
as there is no member property equals
on the String prototype.
To pare two strings use equality operator ==
or strict equality operator ===
.
if (cc === "GB") {
Also, in the else
block, there is missing quote.
It should be
console.log("You're not in the UK")
^
Here's plete code:
var cc = country_code;
if (cc === "GB") { // Use equality operator to pare strings
console.log("You're in the UK");
} else {
console.log("You 're not in the UK"); // Added missing quote
}
var cc = country_code;
if (cc == "GB"){
console.log("You're in the UK");
}
else {
console.log("You're not in the UK");
}
The reason why this was happening was because the text response from my XMLHttpRequest object was actually "GB " - three characters long.
I just cut it down to two and it worked fine: cc.substring(0,2)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745293820a4621021.html
评论列表(0条)