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?

user2109254
  • 1,709
  • 2
  • 30
  • 49
  • 2
    `1 == 01` and `1 === 01` are both true in my browser (Chrome) – Phil May 22 '15 at 01:31
  • 1
    I tried in firefox console, but 1 == 01 (true) 1 === 01 (true) – andyf May 22 '15 at 01:31
  • 4
    Are you sure you didn't actually try `10 == 010`? – Barmar May 22 '15 at 01:32
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Octal – epascarello May 22 '15 at 01:34
  • 4
    `010` is octal, it's equivalent to `8`. – Barmar May 22 '15 at 01:34
  • 2
    The question probably should be "Why is `10 != 010` ?" – RobG May 22 '15 at 01:44
  • This is an exact duplicate of [Javascript behavior with numbers](http://stackoverflow.com/questions/26628507/javascript-behavior-with-numbers), which has been closed as a duplicate of [In JavaScript, eval(010) returns 8](http://stackoverflow.com/questions/6718102/in-javascript-eval010-returns-8). I'm voting to close this question as a dupe of the latter. – chue x May 22 '15 at 03:36

2 Answers2

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
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
  • 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 '15 at 03: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
Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    Just nitpicking, the first arg to parseInt should be a string, it could affect the result. –  May 22 '15 at 01: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 '15 at 01: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 –  May 22 '15 at 01:44
  • 1
    @K3N good thing I didn't pass in strings then – Phil May 22 '15 at 01: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 '15 at 02:15