Skip to content

SyntaxError

In JavaScript, SyntaxError is the native error type that the engine throws when it can’t parse source code, JSON text, or a regular expression pattern. It extends Error, so it carries a message, a name, and a stack trace. In a checkout script with a missing operand, the whole file fails before its first line runs:

Language: JavaScript Filename: checkout.mjs
console.log("Starting checkout");
const total = 10 +;

Node.js prints the error and nothing else, because the console.log() call never runs:

Language: Shell
$ node checkout.mjs
SyntaxError: Unexpected token ';'

When JavaScript Throws SyntaxError

Most SyntaxError exceptions are early errors, which the engine reports while parsing, before any code in the script or module runs. A missing bracket, a stray token, a duplicate const name, or an await in a function that isn’t async stops the whole file.

At run time, JSON.parse() throws one for invalid JSON, RegExp() for an invalid pattern, and eval() and new Function() for invalid source strings. Code can also throw one on purpose, with the same arguments that Error() takes.

Catching SyntaxError

A parse-time SyntaxError can’t be caught by the code that contains it, because the engine rejects the whole file before any try statement in it runs. Only code that parses other text at run time can catch one, such as a call to JSON.parse() or eval(), or a dynamic import() of a broken module.

A server response may be an HTML error page instead of JSON. This function falls back to default settings when the text doesn’t parse and rethrows any other error:

Language: JavaScript
> function parseSettings(text) {
…   try {
…     return JSON.parse(text);
…   } catch (error) {
…     if (error instanceof SyntaxError) {
…       return { theme: "light" };
…     }
…     throw error;
…   }
… }
undefined

> parseSettings("<!DOCTYPE html>")
{ theme: 'light' }

> parseSettings('{"theme": "dark"}')
{ theme: 'dark' }

The instanceof check fails for errors from another realm, such as an iframe or a node:vm context, while error.name and Error.isError() still work.

Common Messages

These are the messages that SyntaxError carries most often in Chrome and Node.js:

Message (V8) Usual Cause Fix
Unexpected token 'x' A stray character, or an operator with nothing after it Remove the token or add what’s missing
Unexpected end of input An unclosed {, (, [, or template literal Close the bracket or literal that’s still open
Invalid or unexpected token An unclosed string or a pasted curly quote Close the string or retype the quote
Cannot use import statement outside a module An import declaration in a CommonJS file, or in a <script> without type="module" Use a .mjs file, or set the module type in package.json or the <script> tag
await is only valid in async functions and the top level bodies of modules An await in a function that isn’t async. ES modules report it as Unexpected reserved word Mark the enclosing function async
Identifier 'x' has already been declared Two let, const, or class declarations of one name in one scope Rename one of them
Unexpected token 'x', "y"... is not valid JSON JSON.parse() on text that isn’t JSON, often an HTML page that starts with <!DOCTYPE Check the response status before parsing
Unexpected end of JSON input JSON.parse() on an empty string, such as an empty response body Check for empty text first

Firefox and Safari word some of these messages differently:

V8 Firefox (SpiderMonkey) Safari (JavaScriptCore)
Unexpected end of input missing } after function body Unexpected end of script
Cannot use import statement outside a module import declarations may only appear at top level of a module Unexpected identifier 'x'. import call expects one or two arguments.
Unexpected token 'x', "y"... is not valid JSON JSON.parse: unexpected character at line x column y of the JSON data JSON Parse error: Unrecognized token 'x'
Unexpected end of JSON input JSON.parse: unexpected end of data at line x column y of the JSON data JSON Parse error: Unexpected EOF

Node.js 18 and earlier printed Unexpected token < in JSON at position 0 for the HTML-page case, and Node.js 19 switched to the current wording.

When the JSON text starts with <, the server sent an HTML page, so check the request URL and the response status before parsing.

A developer diagnoses warning cards using a machine labelled TRY, CATCH and FINALLY.

Course

JavaScript Error Handling in Depth for Pythonistas

Port a food bank's all-or-nothing donation import from Python to JavaScript, and learn to define error classes, chain errors with cause, report many errors at once, and use a debugger.

intermediate js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026