O
O
Ord EO2019-02-01 18:36:32
JavaScript
Ord EO, 2019-02-01 18:36:32

Why does the console return a number in octal when writing a number with a leading zero?

Why does the console return a number in octal when writing a number with a leading zero?
I understand that by putting 0 in front of the number, the browser starts to perceive the number not as decimal but as octal, but why exactly 8, and not binary, for example?
5c5467481717b530633377.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zendor, 2019-02-01
@OrdeO

Yes, you understood everything correctly, use predicates (0b - binary, 0o - octal, 0x - hexadecimal),
in strict mode (starting from ES5) it is generally forbidden to use an octal literal without a predicate:

function foo() {
  "use strict";
  0321;
}
//Uncaught SyntaxError: Octal literals are not allowed in strict mode.

And this happens because according to the specification, all numbers with leading zeros are regarded as numbers in the octal number system (but not in strict mode, see above), most likely this behavior was inherited from C / C ++.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question