Octal literals are not allowed because disallowing them discourages programmers from using leading zeros as padding in a script. For example, look at the following snippet:
var eight = 0008, nine = 00009, ten = 000010, eleven = 011; console.log(eight, nine, ten, eleven);
Seems harmless enough, right? We programmers with OCD just want to align all the commas together so it looks nicer. But here’s the problem:
8 9 8 9
This is the output. See how inconsistent it becomes? Not all zero-padded numeric literals will convert to octal, since 8 and 9 are not octal digits. It’s harder to keep them consistent when having to remember all these rules, so
strict mode makes it easier by disallowing it altogether.
Instead you should pad with leading spaces, or if you want to use octal, then utilize
parseInt() with the optional
radix argument of
8 to specify octal.
Here are the two “solutions”, respectively:
"use strict"; var eight = 8, nine = 9, ten = 10, eleven = 11; console.log(eight, nine, ten, eleven);
"use strict"; var eight = parseInt('010', 8), nine = parseInt('011', 8), ten = parseInt('012', 8), eleven = parseInt('013', 8); console.log(eight, nine, ten, eleven);