14

Why is it in JavaScript that the following results in false:

10 === 000000010 (false)

But this results in true:

010 === 000000010 (true)

In all cases the left and right are both 10, they should all result in true shouldn't they?

7
  • 2
    1 == 01 and 1 === 01 are both true in my browser (Chrome)
    – Phil
    May 22, 2015 at 1:31
  • 1
    I tried in firefox console, but 1 == 01 (true) 1 === 01 (true)
    – andyf
    May 22, 2015 at 1:31
  • 4
    Are you sure you didn't actually try 10 == 010?
    – Barmar
    May 22, 2015 at 1:32
  • 4
    010 is octal, it's equivalent to 8.
    – Barmar
    May 22, 2015 at 1:34
  • 2
    The question probably should be "Why is 10 != 010 ?"
    – RobG
    May 22, 2015 at 1:44

2 Answers 2

23

JavaScript numbers beginning with leading 0s followed by any of the digits 01234567 are octal (base 8) rather than in decimal (base 10).

You can see this in an example like this:

10 === 010 // false
8 === 010 // true

Note that if there is an 8 or 9 digit, it is not a valid octal number and thus will be interpreted as a decimal number:

89 === 089 // true

Note that octal literals don't work in strict mode:

(function(){ "use strict"; return 010 === 10; })()
// SyntaxError: Octal literals are not allowed in strict mode.

This is described in section B.1.1 of the JavaScript specification as non-normative behavior for compatibility with older versions of ECMAScript. An octal integer literal is defined as follows:

OctalIntegerLiteral ::
    0 OctalDigit
    OctalIntegerLiteral OctalDigit

OctalDigit :: one of
    0 1 2 3 4 5 6 7
1
  • 2
    "if there is an 8 or 9 digit, it ... will be interpreted as a decimal number" Javascript would do that, wouldn't it.
    – Random832
    May 22, 2015 at 3:38
8

Your current example not-withstanding, numbers prefixed with a 0 that only contain the digits 0-7 are interpreted as octal. A better example would be

123 == 0123 // nope

because 0123 in base 10 is 83.


To bring it inline with your updated example

parseInt(10, 10) // 10
parseInt(000000010, 10) // 8
parseInt(010, 10) // 8
5
  • 1
    Just nitpicking, the first arg to parseInt should be a string, it could affect the result.
    – user1693593
    May 22, 2015 at 1:42
  • 1
    @K3N From the docs ~ "The value to parse. If string is not a string, then it is converted to one. Leading whitespace in the string is ignored.". I think it's safe and was mainly just to show OP what each value is in base10
    – Phil
    May 22, 2015 at 1:43
  • 2
    @Phil try running sure, it cast them, but if you provided those numbers as string the result would be different (ie. parseInt("000000010", 10) // = 10 just sayin
    – user1693593
    May 22, 2015 at 1:44
  • 1
    @K3N good thing I didn't pass in strings then
    – Phil
    May 22, 2015 at 1:45
  • 1
    @K3N: But parseInt(010, 10) does the same as parseInt(8, 10). You can try parseInt(010, 10) vs parseInt("010", 10) to show a difference, but using parseInt just doesn't make sense in a question about the values of literals.
    – Bergi
    May 22, 2015 at 2:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.