CKTIS / JavaScript
SyntaxError — syntax errors
SyntaxError indicates a parsing failure in code or text that requires a particular syntax, such as JSON. Learn the difference between an error that prevents a script from running and an exception thrown during execution.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
new SyntaxError(message)
SyntaxError(message)
throw new SyntaxError(message)SyntaxError is a built-in ECMAScript error type that inherits from Error. It signals a parsing problem; it is not a separate Web API. For example, invalid JavaScript code can be rejected before execution, while JSON.parse() throws this exception for invalid JSON text.
new SyntaxError(message) creates an object but does not throw it; use throw for that. The message parameter is optional. Calling SyntaxError(message) without new also creates an error object. The default name is "SyntaxError", and message describes the problem.
A try...catch block cannot catch a syntax error that prevents the same script from being parsed: that script never starts executing. It can, however, catch a SyntaxError thrown during execution, such as one from JSON.parse() parsing text supplied as an argument.
In the example, a JSON value is missing after "count":. The JavaScript code itself remains valid, so catch can handle the exception. The instanceof SyntaxError check recognizes this type within the same realm; objects from another realm, such as another window, may fail the check. Do not match automatically generated message strings because their wording can differ between engines.
Examples
try {
JSON.parse('{"count": }');
} catch (error) {
if (!(error instanceof SyntaxError)) {
throw error;
}
console.log(error.name);
}Result
SyntaxError
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.