-1

with javascript when you compare 2 strings what are you comparing exactly

return "hello" > "hola"

this will return false, why ?

zied.hosni
  • 490
  • 1
  • 5
  • 19
  • 1
    because "o" is greater than "e" in ascii code, or more specifically, unicode. – dandavis Dec 30 '15 at 20:51
  • The strings, more accurately, a string's code points. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Comparing_strings, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare, etc. – Dave Newton Dec 30 '15 at 20:56
  • https://en.wikipedia.org/wiki/Lexicographical_order – Barmar Dec 30 '15 at 20:57
  • See the [*Abstract Relational Comparison* algorithm](http://ecma-international.org/ecma-262/6.0/index.html#sec-abstract-relational-comparison). – RobG Dec 30 '15 at 21:13

2 Answers2

0

The strings are compared character-by-character (h vs h), then (e vs o) until either one (or both) of the strings ends, or you get an inequality. In this case 'e' is less than 'o'.

Every character is represented as a number.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
0

The comparison in javascript strings first compares characters one by one from the beginning. An string is bigger than another when the same index characters of the first different character in both strings is bigger than another.

"hello" > "hola"
"h" == "h"
"e" != "o" --> "e" < "o"

this is why the result is false;

Navid Nabavi
  • 532
  • 6
  • 12