Error Reference › JavaScript
JavaScript
Unexpected token (SyntaxError)
The JavaScript (or JSON) parser hit a character it didn't expect — usually a typo in the syntax.
What it looks like
Uncaught SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
What does "Unexpected token (SyntaxError)" mean?
Thrown either by the JavaScript parser (a real syntax error in a file) or by JSON.parse and response.json() (the text was not JSON). An unexpected < almost always means an HTML page — often an error or login page — arrived where JSON was expected.
What causes "Unexpected token (SyntaxError)"?
- A missing/extra bracket, comma, or quote.
- Pasting HTML/an error page where JSON was expected.
- Using new syntax an old engine doesn't support.
How do I confirm the cause?
- If the message mentions JSON, open the request in the Network tab and look at the raw response — it is probably HTML.
- For a code error, go to the line and column given and check the characters just before it: a missing bracket, comma or quote.
- If the code runs in some browsers and not others, check for syntax newer than the oldest browser you support.
How do I fix "Unexpected token (SyntaxError)"?
- Look at the line/column the error points to and just before it.
- Run it through a linter or JSON validator.
- Check the response was actually JSON, not an HTML error page.
How do I stop it happening again?
- Check response.ok and the Content-Type before calling response.json().
- Run a linter or formatter in your editor so syntax errors show up before you save.