What is the cause? operators or use of strings? [duplicate]

爱⌒轻易说出口 提交于 2020-01-05 17:58:15

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/27072578/what-is-the-cause-operators-or-use-of-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!