29

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?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
  • 2
    [Related](http://stackoverflow.com/questions/23609042/how-to-avoid-octal-literals-are-not-allowed-in-strict-mode-with-createwritestr) - has the workarounds. Doesn't explain the _why_ they're not allowed though. – James Thorpe Dec 18 '15 at 14:58
  • As for the _why_, because [the specification](http://www.ecma-international.org/ecma-262/5.1/#sec-B.1.1) says so. – James Thorpe Dec 18 '15 at 15:00
  • 5
    Why is obvious isn't it? It causes much confusion to people as in normal life the addition of a leading zero to a base 10 number doesn't change its value (e.g. from 10 to 8.) – Martin Smith Dec 18 '15 at 15:01
  • @MartinSmith so is the confusion **only** reason for not allowing Octals? – Zameer Ansari Dec 18 '15 at 15:03
  • 1
    [Here's another question](http://stackoverflow.com/questions/2547836/why-doesnt-an-octal-literal-as-a-string-cast-to-a-number) relating to how octals cause inconsistencies in the language - the accepted answer also describes why they were removed too. – James Thorpe Dec 18 '15 at 15:05
  • 1
    @student not a great loss, is it? Other than Unix file permissions, I don't think anyone uses octal these days. – jcaron Dec 18 '15 at 15:08
  • @JamesThorpe ["0100" gives 100, not 64`](http://stackoverflow.com/q/2547836/2404470) is looking like an edge case to me – Zameer Ansari Dec 18 '15 at 15:08
  • @jcaron Just out of curiosity - how did you connected this question to **Unix file permissions**? – Zameer Ansari Dec 18 '15 at 15:10
  • 2
    @student that's the only case that comes to mind where people use octal, as it makes sense there due to the 3-bit grouping (e.g. `chmod 644 file`). I'd be curious to see any other prevalent use case in the past 10 years (probably even more). – jcaron Dec 18 '15 at 15:14
  • @jcaron I have had to use octal on many occasions in the past year, not related to unix permissions. It is used for various things in unix and linux consoles, as well as a great timesaver when dealing with lots of certain kinds of data (e.g. working with 100MM+ records). It's sometimes frustrating not to have octal in strict. – Sir Robert Jan 24 '17 at 21:07

6 Answers6

26

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);
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • 3
    (from another programmer with OCD) This is the clearest answer to the OP's "What is the harm?" - and the best solution for padding for alignment. – Velojet Jul 08 '18 at 05:35
  • I hope this isn't the real reason. At the end of the day it doesn't matter 'cause you can just abstract out the language entirely and make sure `parseInt` happens by itself, but why not just make `001` a syntax error and `01` a valid octal literal? – KernelDeimos Mar 18 '20 at 18:46
  • @KernelDeimos in strict mode, any multi-digit integer with a leading zero is a syntax error. If you want octal you should use the `0o` prefix. I don't know off-hand which specification introduced that syntax though. – Patrick Roberts Mar 18 '20 at 19:04
18

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);
Amit
  • 45,440
  • 9
  • 78
  • 110
15

Nowadays, with large browser support to ES6, you could write this:

const NINE = 0o11; // octal
const TEN = 0b1010; // binary
const SEVENTEEN = 0x11; // hexa
Willian Balmant
  • 167
  • 1
  • 3
5

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.

gsnedders
  • 5,532
  • 2
  • 30
  • 41
4

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.

  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/30349595) – Abilogos Nov 16 '21 at 16:23
0

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);