Skip to content
engineering workbench.by StudyPort Academy
All errors
syntax · DEBUG LIBRARY

SyntaxError: Unexpected token

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

The parser found a token that does not fit the grammar at that location. A missing comma, unmatched bracket, unfinished string, or stray delimiter can make the next otherwise sensible token look unexpected.

The flagged position is where parsing became impossible, not necessarily where the mistake began. Read the preceding line and match the surrounding brackets before changing the marked word.

parsing has a related source of confusion: JSON text is stricter than JavaScript literals. Unquoted keys, single-quoted strings, comments, and trailing commas are not valid JSON. Exact error wording varies by engine and token.

Minimal reproduction

A missing comma in JavaScript

const point = {
  x: 1
  y: 2
};

Object-literal syntax inside JSON text

const point = JSON.parse('{x: 1}');

The fix

Repair the according to the parser that is reading it. For JavaScript, use formatting and bracket matching to check the surrounding . For , use double-quoted keys and strings and remove trailing commas.

If .parse receives a network body, inspect the actual response and content type too. An HTML error page beginning with < is not JSON, and changing a bracket in your application cannot repair that server response.

Separate the properties

const point = {
  x: 1,
  y: 2
};
console.log(point.y);

Supply actual JSON text

const point = JSON.parse('{"x":1}');
console.log(point.x);