Horje
JavaScript SyntaxError – Octal literals are not allowed in strict mode

If you put an octal literal in a code block that runs on strict, the result may be SyntaxError: Octal literals are not allowed in strict mode, since they can lead to unintended actions and confusion, numbers such as 0123 should never be strictly used, to understand this error better including possible causes, consider these examples.

Understanding an error

In JavaScript, strict mode is a way of voluntarily opting into a restricted variant of JavaScript for example catching common mistakes in coding and illegal actions like defining global variables, one of the restrictions is the prohibition against using octal literals in syntax, in earlier versions of JavaScript, octal literals were deprecated from the program’s structure when it was put in a stricter mode to avoid ambiguity.

Case 1: Using Octal Literals in Strict Mode

Error Cause:

When an octal literal is used in strict mode a syntax error is raised because syntax errors related to octal literals are not allowed when using strict mode.

Example:

"use strict";
let num = 0123;
console.log(num);

Output:

SyntaxError: Octal literals are not allowed in strict mode.

Resolution of error:

To avoid such error you should not use octal literals in strict mode to solve this problem, it can be represented by its decimal format or another notation for octal numbers.

JavaScript
"use strict";
let num = 123; // Decimal representation
console.log(num);

Output
123

Case 2: Using Octal Literals with the 0o Prefix

Error Cause:

Use 0o as the prefix of octal literals if you want to make them explicit in strict mode.

Example:

"use strict";
let num = 0123;
console.log(num);

Output:

SyntaxError: Octal literals are not allowed in strict mode.

Resolution of error:

Use 0o for Octal Literals under Strict Mode.

JavaScript
"use strict";
let num = 0o123; // Octal representation using 0o prefix
console.log(num);

Output
83

Case 3: Avoiding Implicit Strict Mode

Error Cause:

Strict mode could also be implicitly set on modules, a similar consequence will result from coding with an octal literal in such settings.

Example:

// Assuming this is in a module
let num = 0123;
console.log(num);

Output:

SyntaxError: Octal literals are not allowed in strict mode.

Resolution of error:

Use the 0o prefix for all your module octal literals or don’t use module any at all.

JavaScript
// Assuming this is in a module
let num = 0o123;
console.log(num);

Output
83

Conclusion

Avoid old-style octal literals (leading zero) in strict mode to prevent “Octal literals are not allowed in strict mode” JavaScript errors, use the 0o prefix for an octal value instead or convert numbers into decimal or hexadecimal representation, this is important for clarity, unambiguity and good practice with modern JavaScript standards.




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript SyntaxError – Unexpected end of input JavaScript SyntaxError – Unexpected end of input
Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript
Difference Between Bearer Token and Basic Authentication Difference Between Bearer Token and Basic Authentication
How to Break Statement in an Array Map Method in JavaScript? How to Break Statement in an Array Map Method in JavaScript?
Difference Between in and hasOwnProperty in JavaScript Difference Between in and hasOwnProperty in JavaScript

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
20