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?
asked Dec 18, 2015 at 14:55
Zameer AnsariZameer Ansari
27.7k22 gold badges136 silver badges215 bronze badges
10
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:
answered Dec 18, 2015 at 15:14
Patrick RobertsPatrick Roberts
47.8k8 gold badges97 silver badges149 bronze badges
3
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);
answered Dec 18, 2015 at 14:58
Nowadays, with large browser support to ES6, you could write this:
const NINE = 0o11; // octal
const TEN = 0b1010; // binary
const SEVENTEEN = 0x11; // hexa
answered Dec 20, 2017 at 1:19
0
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.
answered Dec 18, 2015 at 15:06
gsneddersgsnedders
5,4722 gold badges30 silver badges41 bronze badges
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.
answered Nov 15, 2021 at 20:15
1
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);
answered Apr 27, 2022 at 10:35
Maybe you are using wrong String-literals( » ), for JSONs in «strict-mode» in JS.
Correct : {
«name»: «Josh»
}
Wrong : {
‘name’: ‘Josh’
}
answered Jun 24, 2021 at 7:42
1
Asked
1 year, 6 months ago
Viewed
3k times
I am trying to loops through these array’s but am getting this error «Legacy octal literals are not allowed in strict mode»
const myList =[
{
id: 01,
title: 'FrontEnd Engineer',
name : 'Sheshathri',
describtion: "simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"
},
{
id: 02,
title: 'QA Engineer',
name : 'Jobin',
describtion: "It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
}
]
export default myList;
In App.js
import myList from './ContentList';
const listItem = myList.map(list => <Content key= {list.id} title = {list.title} name = {list.name} describtion = {list.describtion} />);
asked Jul 31, 2021 at 16:45
1
I am trying to loops through these array’s but am getting this error
«Legacy octal literals are not allowed in strict mode»
It refers to yours ids:
id: 01,
title: 'FrontEnd Engineer',
Replace with
id: 1,
title: 'FrontEnd Engineer',
Octal literals are numerals that begin with a leading zero, such as:
var num = 071; // 57
The leading zero to identify an octal literal has been a source of
confusion and error in JavaScript. ECMAScript 5 deprecates the use of
octal numeric literals in JavaScript and octal literals cause syntax
errors in strict mode.
Octal system
answered Jul 31, 2021 at 16:46
Giorgi MoniavaGiorgi Moniava
26.1k7 gold badges52 silver badges88 bronze badges
0
History
This warning has existed in three forms across the three main linters. It was
introduced in the original version of JSLint and has remained (in a way) in all
three tools ever since.
-
In JSLint the warning given is «Don’t use octal: ‘{a}’. Use ‘u…’ instead»
-
In JSHint and ESLint the warning has always been «Octal literals are not
allowed in strict mode»
The situations that produce the warning have not changed despite changes to the
text of the warning itself.
When do I get this error?
The «Don’t use octal: ‘{a}’. Use ‘u…’ instead» error, and the alternative
«Octal literals are not allowed in strict mode», is thrown when JSLint,
JSHint or ESLint encounters a string literal that contains the escape
character followed by a digit between 0
and 7
. JSHint and ESLint will only
raise this warning when the relevant code is running in strict mode. In the
following example we attempt to assign a string containing an octal escape to a
variable x
:
function demo() { "use strict"; return "Copyright 251"; }
Why do I get this error?
This error is raised to highlight the use of a deprecated language feature.
As of version 5 of the ECMAScript specification, octal escape sequences are
deprecated and should no longer be used. You run the risk of losing
compatibility with newer JavaScript engines as support for this feature is
gradually dropped. The latest version of the specification contains the
following note (ES5 B.1):
Past editions of ECMAScript have included additional syntax and semantics for
specifying octal literals and octal escape sequences. These have been removed
from this edition of ECMAScript.
If you need to use an escape sequence, you can use hexadecimal or unicode
sequences. JSLint recommends that you use the unicode escape sequence over the
hexadecimal equivalent. The reason for this is likely that unicode escape
sequences can cover a vast range of characters compared to the 256 offered by
hexadecimal sequences, and that the JSON specification only allows unicode
escapes. Note that although they have different names, both hexadecimal and
unicode escape sequences use hexadecimal numbers. Here’s the above example
again, using a unicode escape instead:
function demo() { "use strict"; return "Copyright u00A9"; }
However, if you would rather use the hexadecimal escape sequence, none of the
three main linters will ask you to do otherwise:
function demo() { "use strict"; return "Copyright xA9"; }
In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. The identifier of this warning is W115.
This means you can tell JSHint to not issue this warning with the /*jshint -W115 */
directive.