This question has already been answered:

I played in the console and noticed the following situation: if you set the variable value to any number (1, 47, -24.1), then this number will be saved to the variable, but if you assign a number to it, like 010, 015, etc., then the variable is saved meaning 8, 13. What is happening here and what is called?

enter image description here

Reported as a duplicate at Grundy. javascript Aug 18 '18 at 4:59 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Because it is taken as an eight-bit number (8), since this number has 0 at the beginning. Thus, the corresponding decimal value is 10 | 012: (2 * 8 ^ 0 + 1 * 8 ^ 1 = 10) - Netahaki
  • one
    @Netahaki not octopus, but octal - Pavel Mayorov
  • Javascript Magic ^. ^ - Flippy

1 answer 1

In ES5, to represent an octal literal, you use a zero prefix (0) followed by a sequence of octal digits (from 0 to 7). See the following example:

var a = 051; console.log(a); // 41 

If the octal literal contains a number outside the acceptable range, JavaScript ignores the leading value 0 and treats the octal literal as a decimal, as shown in the following example:

 var b = 058; // invalid octal console.log(b); // 58 

Read more