Do you know what will be the output of this code?
console.log(018 - 015);
Show answer and explanation
This will actually result in 5 but this is pretty strange, isn’t it?
Please note, that this example will only work in lax mode, and not in strict mode, and we will come to the explanation soon enough.
In earlier versions of JavaScript, a leading 0 on a number marked an octal number.
Octal numbers are numbers with a base of 8 (instead 10 like with decimal numbers).
This means that numbers from 0 to 7 are valid digits.
In our case, 018 can’t actually be an octal number, can it?
Well it can’t and the runtime knows this, which is why it treats 018 as 18 although the right side contains a perfectly valid octal number 015 which is 13 in decimal.
And the runtime will perform the calculation exactly like this, which results in:
18 - 13 -> 5