Why is one string greater than the other when comparing strings in JavaScript?

后端 未结 4 1141
鱼传尺愫
鱼传尺愫 2020-11-22 09:48

I see this code from a book:

var a = \"one\";
var b = \"four\";
a>b; // will return true

but it doesn\'t mention why \"one\" is bigger t

4条回答
  •  星月不相逢
    2020-11-22 09:57

    Because, as in many programming languages, strings are compared lexicographically.

    You can think of this as a fancier version of alphabetical ordering, the difference being that alphabetic ordering only covers the 26 characters a through z.


    This answer is in response to a java question, but the logic is exactly the same. Another good one: String Compare "Logic".

提交回复
热议问题