Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Octal numeric literals not allowed in strict mode (and what is the workaround?)

Tags:

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?

like image 821
xameeramir Avatar asked Dec 18 '15 14:12

xameeramir


People also ask

What are octal literals?

Literals with a leading zero are octal literals. Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1. To define integer literals as octal value in Java is effortless.

What are octal literals in JavaScript?

Octal literals start with 0o followed by a sequence of numbers between 0 and 7. Binary literals start with 0b followed by a sequence of number 0 and 1.

What is use strict JS?

The "use strict" Directive It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you can not, for example, use undeclared variables.

Are octal literals allowed in strict mode?

"use strict"; var x = 010; //Uncaught SyntaxError: Octal literals are not allowed in strict mode. In case a developer needs to use Octals (which can mistakenly change a numbers meaning ), is there a workaround?

What went wrong with octal literals?

SyntaxError in strict mode only. What went wrong? Octal literals and octal escape sequences are deprecated and will throw a SyntaxError in strict mode. With ECMAScript 2015 and later, the standardized syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" ( 0o or 0O) .

Are octal literals deprecated in ECMAScript?

Octal literals and octal escape sequences are deprecated and will throw a SyntaxError in strict mode. With ECMAScript 2015 and later, the standardized syntax uses a leading zero followed by a lowercase or uppercase Latin letter "O" ( 0o or 0O).

What does the JavaScript strict mode-only exception 0o mean?

The JavaScript strict mode -only exception "0-prefixed octal literals and octal escape sequences are deprecated; for octal literals use the "0o" prefix instead" occurs when deprecated octal literals and octal escape sequences are used. SyntaxError in strict mode only. What went wrong?


1 Answers

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);
like image 88
Patrick Roberts Avatar answered Oct 01 '22 01:10

Patrick Roberts