Reputation: 93
I have a condition in javascript like this :
var a = '60';
var b = '500';
if(a < b) {
console.log('true');
} else {
console.log('false');
}
but the result is false, my expectation should be true, but i tried to compare with php code :
<?php
$a = '60';
$b = '501';
if($a < $b) {
echo 'true';
} else {
echo 'false';
}
?>
and the result is true, if in javascript there is no else if condition it will automatically read to the false condition if the value is not true?
Upvotes: 0
Views: 101
Reputation: 2840
If you're expecting a
and b
to behave like numbers, don't put them in quotes. That makes them strings.
var a = 60;
var b = 500;
PHP automatically converts those to a number. Try running echo $a - 1;
You'll get 59
. Also try "00001" == "1"
. You'll get true! JavaScript doesn't do this kind of detection. Instead JavaScript compares strings alphabetically by their char codes. By alphabetically, I mean that it compares the first characters of each string. If they have the same char codes, it moves on to the next character of each string. If one has a higher char code than the other, it is "greater" than the other string- much like we do when we're determining if "dog" or "dot" comes first alphabetically. In this case, JavaScript would see that 6
has a char code of 54
, and 5
has a char code of 53
, and it would conclude right then and there that "60"
is greater than "500"
.
Upvotes: 5
Reputation: 101
this is not the case Javascript supports if,else if and else statements but in your code you have declared the values as strings so the compiler is not comparing the strings directly so you have to pass your variables a and b to "Number" function of javascript which will convert strings to integer.
var a = '60';
var b = '500';
var aint = Number(a);
var bint = Number(b);
if(aint < bint) {
console.log('true');
}
else if(aint>bint){
console.log('a is greater');
}else {
console.log('false');
}
Output: true
Upvotes: 0
Reputation: 111
Good question. The if-else statement is correct, but the variables are not set correctly.
You can use var, but you should use let or const. (but that is just a semantic thing)
The REAL reason you are having issues is that you are putting your numbers as strings..so literally, '60' is not less than '500'.
var a = 60;
var b = 500;
in this case it would come out true. 60 is, in fact, less then 500 =).
Keep up the good work.
Upvotes: 1
Reputation: 12027
PHP converts both strings to numbers before comparing them. If you want the same in JavaScript, use:
if (parseInt(a, 10) < parseInt(b, 10)) {
This is documented:
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is
===
or!==
as this involves comparing the type as well as the value.
Upvotes: 2
Reputation: 7749
You should avoid adding quotes in numbers but in case it is present there then you
can convert to number using parseInt
.
Try this:
var a = '60';
var b = '500';
if(parseInt(a) < parseInt(b)) {
console.log('true');
} else {
console.log('false');
}
Upvotes: 1