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
  • 15,899
  • 5
  • 47
  • 67
vikas
  • 2,612
  • 4
  • 24
  • 34

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
  • 221,210
  • 51
  • 432
  • 478
0

Checking that strings are integers is separate to comparing..

See this link: Javascript string/integer comparisons

Community
  • 1
  • 1