JavaScript SyntaxError Object
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
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.
Desktop





Tablets & Mobile





