--- title: SyntaxError slug: Web/JavaScript/Reference/Global_Objects/SyntaxError tags: - Error - JavaScript - Object - Reference - SyntaxError translation_of: Web/JavaScript/Reference/Global_Objects/SyntaxError --- <div>{{JSRef}}</div> <p><code><strong>SyntaxError</strong></code> オブジェクトは、構文的に不正なコードを解釈しようとした場合のエラーを表します。これは、 JavaScript エンジンが、コードを解析中に言語の構文に従わないトークンまたはトークンの順序に遭遇した場合に発生します。</p> <h2 id="Constructor" name="Constructor">コンストラクター</h2> <dl> <dt>{{jsxref("Global_Objects/SyntaxError/SyntaxError", "SyntaxError()")}}</dt> <dd>新しい <code>SyntaxError</code> オブジェクトを生成します。</dd> </dl> <h2 id="Instance_properties" name="Instance_properties">インスタンスプロパティ</h2> <dl> <dt>{{jsxref("Error.prototype.message", "SyntaxError.prototype.message")}}</dt> <dd>エラーメッセージです。 ECMA-262 において {{jsxref("SyntaxError")}} は自身の <code>message</code> プロパティを提供するべきとされていますが、 <a href="/ja/docs/Mozilla/Projects/SpiderMonkey">SpiderMonkey</a> では {{jsxref("Error.prototype.message")}} を継承しています。</dd> <dt>{{jsxref("Error.prototype.name", "SyntaxError.prototype.name")}}</dt> <dd>エラー名です。 {{jsxref("Error")}} から継承しています。</dd> <dt>{{jsxref("Error.prototype.fileName", "SyntaxError.prototype.fileName")}}</dt> <dd>このエラーが発生したファイルのパスです。 {{jsxref("Error")}} から継承しています。</dd> <dt>{{jsxref("Error.prototype.lineNumber", "SyntaxError.prototype.lineNumber")}}</dt> <dd>このエラーが発生したファイル内の行番号です。 {{jsxref("Error")}} から継承しています。</dd> <dt>{{jsxref("Error.prototype.columnNumber", "SyntaxError.prototype.columnNumber")}}</dt> <dd>このエラーが発生した行内の桁数です。 {{jsxref("Error")}} から継承しています。</dd> <dt>{{jsxref("Error.prototype.stack", "SyntaxError.prototype.stack")}}</dt> <dd>スタックトレースです。 {{jsxref("Error")}} から継承しています。</dd> </dl> <h2 id="Examples" name="Examples">例</h2> <h3 id="Catching_a_SyntaxError" name="Catching_a_SyntaxError">SyntaxError のキャッチ</h3> <pre class="brush: js notranslate">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); } </pre> <h3 id="Creating_a_SyntaxError" name="Creating_a_SyntaxError">SyntaxError の生成</h3> <pre class="brush: js notranslate">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 } </pre> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('ESDraft', '#sec-native-error-types-used-in-this-standard-syntaxerror', 'SyntaxError')}}</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <div> <div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div> <p>{{Compat("javascript.builtins.SyntaxError")}}</p> </div> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{jsxref("Error")}}</li> </ul>