SyntaxError

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

SyntaxError オブジェクトは、構文的に不正なコードを解釈しようとした場合のエラーを表します。これは、 JavaScript エンジンが、コードを解析中に言語の構文に従わないトークンまたはトークンの順序に遭遇した場合に発生します。

コンストラクター

SyntaxError()

新しい SyntaxError オブジェクトを生成します。

インスタンスプロパティ

SyntaxError.prototype.message

エラーメッセージです。 ECMA-262 において SyntaxError は自身の message プロパティを提供するべきとされていますが、 SpiderMonkey では Error.prototype.message を継承しています。

SyntaxError.prototype.name

エラー名です。 Error から継承しています。

SyntaxError.prototype.fileName

このエラーが発生したファイルのパスです。 Error から継承しています。

SyntaxError.prototype.lineNumber

このエラーが発生したファイル内の行番号です。 Error から継承しています。

SyntaxError.prototype.columnNumber

このエラーが発生した行内の桁数です。 Error から継承しています。

SyntaxError.prototype.stack

スタックトレースです。 Error から継承しています。

SyntaxError のキャッチ

js
try {
  eval("hoo bar");
} catch (e) {
  console.error(e instanceof SyntaxError);
  console.error(e.message);
  console.error(e.name);
  console.error(e.fileName);
  console.error(e.lineNumber);
  console.error(e.columnNumber);
  console.error(e.stack);
}

SyntaxError の生成

js
try {
  throw new SyntaxError("Hello", "someFile.js", 10);
} catch (e) {
  console.error(e instanceof SyntaxError); // true
  console.error(e.message); // Hello
  console.error(e.name); // SyntaxError
  console.error(e.fileName); // someFile.js
  console.error(e.lineNumber); // 10
  console.error(e.columnNumber); // 0
  console.error(e.stack); // @debugger eval code:3:9
}

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-native-error-types-used-in-this-standard-syntaxerror

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
SyntaxError
SyntaxError() constructor
SyntaxError is serializable

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.

関連情報