javascript エラー「Uncaught SyntaxError: Octal literals are not allowed in strict mode」の解決方法

javascript エラー「Uncaught SyntaxError: Octal literals are not allowed in strict mode」の解決方法

javascriptで、エラー「Uncaught SyntaxError: Octal literals are not allowed in strict mode」が発生した場合の原因と解決方法を記述してます。このエラーは厳格モード「use strict」を使用時のみ発生します。8進数の表記の仕方が間違っている場合などに発生します。

環境

  • OS windows11 pro 64bit
  • Apache 2.4.43
  • ブラウザ chrome 108.0.5359.125

エラー内容

以下のコードで発生。

"use strict";

let n = 011;

console.log(n)

エラーメッセージ
※use strictの厳格モードのみ発生します。

Uncaught SyntaxError: Octal literals are not allowed in strict mode.

画像

firefox(バージョン107)でも、以下のエラーとなります。

Uncaught SyntaxError: "0"-prefixed octal literals are deprecated; use the "0o" prefix instead

画像

原因

8進数の表記方法が間違っているため

解決方法

数値の前に小文字の「o」か大文字の「O」を使用する

"use strict";

let n = 0o11;

console.log(n)

or

"use strict";

let n = 0O11;

console.log(n)

実行結果