Why does this happen with strings in javascript?
3<=255
true
but
'3'<='255'
false
Is it something to do with the operators or the use of strings?
Why does this happen with strings in javascript?
3<=255
true
but
'3'<='255'
false
Is it something to do with the operators or the use of strings?
I guess it is because it compare ascii values of chars and 3 had greater ascii value than 2. In string it compare char by char if 1 char is false it wont compare else
In first case you are comparing 2 Numbers, in second you are comparing 2 strings. So they are different types and thus produces different results.
Both.
When the comparison is done on numbers, the values of the numbers determine the outcome.
When the comparison is done on strings, the sort order of the strings determine the outcome.
The string '255'
is considered smaller than the string '3'
, because it would come before it in a sorted list.