Favorites

JavaScript SyntaxError Object

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
SyntaxError
JavaScript Errors · ConstructorThis object is supported by all modern browsers.
Updated 28th November 2026

A SyntaxError is thrown when code or data can't be parsed, such as invalid JavaScript or a string passed to JSON.parse() that isn't valid JSON.

Description

A SyntaxError means the JavaScript engine found text it can't parse. Errors in your script's own code are reported before it runs, so they can't be caught by the same script. At run time, a SyntaxError is thrown by functions that parse strings: JSON.parse() with invalid JSON, new RegExp() with an invalid pattern, and eval() or new Function() with invalid code.

When a syntax error is in your own script, the browser reports it in the console as soon as it tries to load the file, and none of that script runs. The message points to the line where parsing failed, though the real mistake, such as a missing bracket or quote, is often earlier. Modules behave the same way, and an error in one module stops every module that imports it from running.

The errors you can catch come from code that parses text at run time. The most common is JSON.parse() on data from a server or localStorage that isn't valid JSON, so wrapping it in try...catch lets you fall back gracefully. Linters and editors catch most syntax mistakes in your own code before it ever reaches the browser.

Creating

new SyntaxError(message)

Example

try {
  JSON.parse('{bad json}');
} catch (err) {
  console.log(err.name);
}

try {
  new RegExp('(unclosed');
} catch (err) {
  console.log(err instanceof SyntaxError);
}

Browser Support

The following information will show you the current browser support for the SyntaxError object. Hover over a browser icon to see the version that first introduced support for this object.

This object is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee