I simply cannot wrap my head around this:
2590.00 > 2.00 //== true
"2590.00" > "2.00" //== true
105.00 > 2.00 //== true
"105.00" > "2.00" //== false???
Why is the last expression returning false
?
I simply cannot wrap my head around this:
2590.00 > 2.00 //== true
"2590.00" > "2.00" //== true
105.00 > 2.00 //== true
"105.00" > "2.00" //== false???
Why is the last expression returning false
?
"105.00" > "2.00"
is comparing strings instead of numbers.
When comparing two strings, "2.00" will be greater than "105.00", because (alphabetically) 105.00 is less than 2.00.
When comparing a string with a numeric constant, JavaScript will treat the number as a string when doing the comparison. The result of this is commonly not the same as a numeric comparison. To secure a proper result, variables should be converted to the proper type before comparison:
Parse the string into an integer using parseInt:
javascript:alert(parseInt("105.00")>parseInt("2.00"));
"105.00" > "2.00"
is comparing the strings "105.00"
and "2.00"
and since '1' < '2'
it returns false