2

How internally JavaScript compare?

alert(022 > "21"); // false
alert(22 > "21"); // true
alert("22" > "21"); // true

I was reading one article here, and it looks that according to that the first should be true.

Huangism
  • 16,278
  • 7
  • 48
  • 74
vikas
  • 2,780
  • 4
  • 27
  • 37

2 Answers2

2

In JavaScript, any numeric literal starting with 0 is considered as an octal number. So

console.log(022);
# 18

That is why console.log(022 > "21"); evaluates to false.

If you want to know how comparing these two entities work, please check the ECMA 5.1 standard specification for The Abstract Relational Comparison Algorithm

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

Checking that strings are integers is separate to comparing..

See this link: Javascript string/integer comparisons

Community
  • 1
  • 1