'Why are Octal numeric literals not allowed in strict mode (and what is the workaround?)
Why are Octal numeric literals not allowed in JavaScript strict mode? What is the harm?
"use strict";
var x = 010; //Uncaught SyntaxError: Octal literals are not allowed in strict mode.
<h1>Check browser console for errors</h1>
In case a developer needs to use Octals (which can mistakenly change a numbers meaning), is there a workaround?
Solution 1:[1]
The "why" part of the question is not really answerable.
As for "how", off the top of my head...
"use strict";
var x = parseInt('010', 8);
document.write(x);
Solution 2:[2]
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);
Solution 3:[3]
Nowadays, with large browser support to ES6, you could write this:
const NINE = 0o11; // octal
const TEN = 0b1010; // binary
const SEVENTEEN = 0x11; // hexa
Solution 4:[4]
Why is Octal numeric literals not allowed in javascript strict mode? What is the harm?
Octals in JS have historically been a non-standard extension to the standard (in ES5, which introduces strict mode, they're in Annex B, which is a collection of non-standard features that most implementations support: except it defines octals in a way incompatible with what websites require), and strict mode made an attempt to disallow all non-standard extensions. The "why" as to why they were never standardised is an obvious related question, and that I'm unaware of.
In case a developer need to use Octals (which can mistakenly change a number's meaning), is there a work around?
As @Amit answered, parseInt
with its second argument as 8 still works in strict mode.
Solution 5:[5]
basically when I was trying to use this format date = new Date(2021,09,07) in react and pass to another component, so that I can convert to ISOString() or toLocaleString(), // {props.toISOString()}. I was getting this error " Legacy octal literals are not allowed in strict mode"
BUT, after removing that "zeros" before month and day change it to date = new Date(2021,9,7) it works completely fine for me.
Solution 6:[6]
if you are applying a strict mode on top of your code you need to place it in this formate
"use strict";
var x = '010';
console.log(x);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Amit |
Solution 2 | |
Solution 3 | Willian Balmant |
Solution 4 | gsnedders |
Solution 5 | Lotfullah Andishmand |
Solution 6 | friday onojah |