Can someone explain why this is the case?
console.log('AB_CD' > 'AB_C_D'); //false
console.log('ab_cd' > 'ab_c_d'); //true
Strings are compared using character codes, which you can learn more about reading this tutorial
In this first comparison ('AB_CD' > 'AB_C_D'
), the character code for D
and _
are 68
and 95
, respectively, which explains why the expression evaluates as false
.
A: 65; A: 65
B: 66; B: 66
_: 95; _: 95
C: 67; C: 67
D: 68; _: 95
In the second comparison ('ab_cd' > 'ab_c_d'
), the character code for d
and _
are 100
and 95
, respectively, which explains why the expression evaluates as true
.
a: 97; a: 97
b: 98; b: 98
_: 95; _: 95
c: 99; c: 99
d: 100; _: 95