javascript エラー「SyntaxError: Unexpected number ‘xxx’. Expected an opening ‘{‘ at the start of a function body.」の解決方法

javascript エラー「SyntaxError: Unexpected number ‘xxx’. Expected an opening ‘{‘ at the start of a function body.」の解決方法

javascriptで、エラー「SyntaxError: Unexpected number ‘xxx’. Expected an opening ‘{‘ at the start of a function body.」が発生した場合の原因と解決方法を記述してます。

環境

  • OS macOS Monterey
  • ブラウザ safari 15.5

エラー内容

以下のコードで発生。

const hoge = function() 1

エラーメッセージ

SyntaxError: Unexpected number '1'. Expected an opening '{' at the start of a function body.

画像

原因

「return」文が記述されていないため

解決方法

「return」文を使用する

const hoge = function() {return 1};