javascript エラー「SyntaxError: Cannot use the reserved word ‘xxx’ as a lexical variable name in strict mode.」の解決方法

javascript エラー「SyntaxError: Cannot use the reserved word ‘xxx’ as a lexical variable name in strict mode.」の解決方法

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]