javascript エラー「Uncaught SyntaxError: Unexpected token ‘,’」の解決方法

javascript エラー「Uncaught SyntaxError: Unexpected token ‘,’」の解決方法

javascriptで、エラー「Uncaught SyntaxError: Unexpected token ‘,’」が発生した際の原因と解決方法を記述してます。

環境

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

エラー内容

以下のコードを実行時に発生

function hog,e() {
  console.log('hoge')
}

エラーメッセージ

Uncaught SyntaxError: Unexpected token ','

画像

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

Uncaught SyntaxError: missing ( before formal parameters

画像

原因

関数名に「,」が入っているため

解決方法

「,」を除く

function hoge() {
  console.log('hoge')
}

ちなみに以下のアロー関数に「,」を使った場合は、

const hog,e = () => {
  console.log('hoge')
}

constに初期値を設定していないエラーと同じエラーが発生します。

Uncaught SyntaxError: Missing initializer in const declaration

上記は、以下と同じエラー

const foo;

Uncaught SyntaxError: Missing initializer in const declaration