Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
79
rated 0 times [  80] [ 1]  / answers: 1 / hits: 28557  / 12 Years ago, sat, june 2, 2012, 12:00:00
if ('11' < '3') alert('true');


It's obvious that it's not comparing them by length but by encoding instead. However, I don't understand how it works. I need some explanation :-)


More From » string

 Answers
4

Strings are compared lexicographicaly. i.e. character by character until they are not equal or there aren't any characters left to compare. The first character of '11' is less than the first character of '3'.



> '11' < '3'
true
> '31' < '3'
false
> '31' < '32'
true
> '31' < '30'
false


If we use letters then, since b is not less than a, abc is not less than aaa, but since c is less than d, abc is less than abd.



> 'abc' < 'aaa'
false
> 'abc' < 'abd'
true


You can explicitly convert strings to numbers:



> +'11' < '3'
false

[#85191] Friday, June 1, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
agustindejonm

Total Points: 738
Total Questions: 84
Total Answers: 84

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
agustindejonm questions
Fri, Jun 25, 21, 00:00, 3 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Sat, May 16, 20, 00:00, 4 Years ago
;