javascript エラー「SyntaxError: Cannot use the reserved word ‘xxx’ as a lexical variable name in strict mode.」の解決方法
- 作成日 2022.04.15
- 更新日 2022.11.14
- javascript
- javascript
javascriptで、エラー「SyntaxError: Cannot use the reserved word ‘xxx’ as a lexical variable name in strict mode.」が発生した場合の原因と解決方法を記述してます。「use strict」で厳格モードの時のみに発生します。
環境
- OS macOS Monterey
- ブラウザ safari 15.5
エラー内容
以下のコードで発生。
'use strict'
const package = [0, 1, 2]
エラーメッセージ
※以下のエラーは「use strict」の厳格モードのみで発生します。
Uncaught SyntaxError: Unexpected strict mode reserved word
画像
原因
厳格モードでは、「package」は、予約語であるためエラーになります。
以下は、javascriptの予約語の一覧となります。
「 break 」
「 case 」
「 catch 」
「 class 」
「 const 」
「 continue 」
「 debugger 」
「 default 」
「 delete 」
「 do 」
「 else 」
「 export 」
「 extends 」
「 finally 」
「 for 」
「 function 」
「 if 」
「 import 」
「 in 」
「 instanceof 」
「 new 」
「 return 」
「 super 」
「 switch 」
「 this 」
「 throw 」
「 try 」
「 typeof 」
「 var 」
「 void 」
「 while 」
「 with 」
「 yield 」
解決方法
厳格モード(use strict)を記述しないか、他の名称に変更する
'use strict'
const package_ = [0, 1, 2]
-
前の記事
Linux シェルで複数行をコメントアウトする 2022.04.14
-
次の記事
jquery 指定した要素のソートを実行する 2022.04.15
コメントを書く