SyntaxError
A syntax error — in code, in JSON or in a regular expression.
Available since: ES3
Syntax
new SyntaxError(message)Parameters
| Parameter | Type | Description |
|---|---|---|
| message | string | Optional. A description of the error. |
Return value
SyntaxErrorAn error object.
Examples
Invalid JSON:
try {
JSON.parse("{'name': 'Anna'}");
} catch (error) {
console.log(error.name);
}Output:
SyntaxErrorAn invalid regular expression:
try {
new RegExp('(abc');
} catch (error) {
console.log(error.name);
}Output:
SyntaxErrorNotes
A syntax error in the program's own code cannot be caught: such code never runs at all. Only errors from parsing data at run time — JSON, regular expressions, new Function() — can be caught.
See also
- JSON.parse — Parses a JSON string and returns the value.
- Error — The base error class: created and thrown with throw.
In the tutorial
- Errors and try...catch — Error handling in JavaScript: try, catch and finally blocks, the error object, the throw statement and built-in error types.