title | slug | tags | browser-compat | |||||
---|---|---|---|---|---|---|---|---|
SyntaxError |
Web/JavaScript/Reference/Global_Objects/SyntaxError |
|
javascript.builtins.SyntaxError |
{{JSRef}}
The SyntaxError
object represents an error when trying to interpret syntactically invalid code. It is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.
SyntaxError
is a {{Glossary("serializable object")}}, so it can be cloned with {{domxref("structuredClone()")}} or copied between Workers using {{domxref("Worker.postMessage()", "postMessage()")}}.
- {{jsxref("Global_Objects/SyntaxError/SyntaxError", "SyntaxError()")}}
- : Creates a new
SyntaxError
object.
- : Creates a new
- {{jsxref("Error.prototype.message", "SyntaxError.prototype.message")}}
- : Error message. Inherited from {{jsxref("Error")}}.
- {{jsxref("Error.prototype.name", "SyntaxError.prototype.name")}}
- : Error name. Inherited from {{jsxref("Error")}}.
- {{jsxref("Error.prototype.cause", "SyntaxError.prototype.cause")}}
- : Error cause. Inherited from {{jsxref("Error")}}.
- {{jsxref("Error.prototype.fileName", "SyntaxError.prototype.fileName")}} {{non-standard_inline}}
- : Path to file that raised this error. Inherited from {{jsxref("Error")}}.
- {{jsxref("Error.prototype.lineNumber", "SyntaxError.prototype.lineNumber")}} {{non-standard_inline}}
- : Line number in file that raised this error. Inherited from {{jsxref("Error")}}.
- {{jsxref("Error.prototype.columnNumber", "SyntaxError.prototype.columnNumber")}} {{non-standard_inline}}
- : Column number in line that raised this error. Inherited from {{jsxref("Error")}}.
- {{jsxref("Error.prototype.stack", "SyntaxError.prototype.stack")}} {{non-standard_inline}}
- : Stack trace. Inherited from {{jsxref("Error")}}.
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);
}
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
}
{{Specifications}}
{{Compat}}
- {{jsxref("Error")}}