0

EDIT : This is not a duplicate of the other question as to realise why prepending 0's to numbers can actually result in a different value, this question has nothing to do with Octals, In the other question does the significance of prepending 0's to a number resulting in a different (Octal) value is not obvious.

For code alignment to look nice, I formatted number of lines that would have an integer as an argument from 1,2,3,.. to 01,02,03,...

Are the two indistinguishable in JavaScript? ( please don't mistake 01 with "01" they are different types. anyone knows of any example in any programming language where 1 and 01 will not be the same value?).

jimjim
  • 2,414
  • 2
  • 26
  • 46
  • 3
    01 and 001 are fine, but watch out for 010. – user2357112 Dec 19 '16 at 03:35
  • Possible duplicate of [Why Octal numeric literals are not allowed in strict mode (and the work-around is?)](http://stackoverflow.com/questions/34358331/why-octal-numeric-literals-are-not-allowed-in-strict-mode-and-the-work-around-i) - this highlights that you *will* get a warning in `strict mode`. – Ken Y-N Dec 19 '16 at 03:37

1 Answers1

6

For this example - yes.

But leading 0s like this is actually octal notation.

01 == 1. But 011 is actually 9.

Basically - you don't want to do this.

Shadow
  • 8,749
  • 4
  • 47
  • 57
  • They certainly don't make sense - but for some reason, javascript is happy to assume you meant base 10 in those cases :/ That is probably one of the main reasons why people think this actually works... – Shadow Dec 19 '16 at 03:38
  • 1
    I would have left it there. Now my comment lacks context :/ – Shadow Dec 19 '16 at 03:39
  • Heh. Here's a different comment. According to the [EcmaScript spec, section 7.8.3](http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.3), octal is not supported in `strict` mode: _"A conforming implementation, when processing [strict mode code (see 10.1.1)](http://www.ecma-international.org/ecma-262/5.1/#sec-10.1.1), must not extend the syntax of NumericLiteral to include OctalIntegerLiteral as described in [B.1.1](http://www.ecma-international.org/ecma-262/5.1/#sec-B.1.1)."_ – Ted Hopp Dec 19 '16 at 03:43
  • Interesting. So that means they are flat out axing literal octals? Interesting. I suppose I can't say I've used octal in javascript, but I'm sure that would annoy someone... – Shadow Dec 19 '16 at 03:45
  • 1
    The parsers are evidently very lenient, but the language spec is very clear that in no case would `08` or `09` be legal literals, and, moreover, in `strict` mode, the only integer literals that can start with a `0` are the literal `0` itself or hex literals, which must start with `0x` or `0X`. – Ted Hopp Dec 19 '16 at 03:49