"Unexpected token" in JavaScript — What It Means and How to Fix It
What the JavaScript error "Unexpected token" means and how to fix it.
Published October 30, 2026
This SyntaxError says the parser met something it did not expect. A very common version is JSON.parse receiving HTML (starting with '<') instead of JSON.
What it means and why it happens
Typical causes: an API returned an HTML error page, wrong URL, or invalid syntax in code.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
SyntaxError: Unexpected token '<' in JSON at position 0
Practical examples
Code that triggers it
JSON.parse('<html>Not found</html>')The fix
const res = await fetch(url)
if (!res.ok) throw new Error(`HTTP ${res.status}`)
const data = await res.json()Inspect the raw response
console.log(await res.text())Common mistakes
- Fetching the wrong URL and getting a 404 page.
- Not checking res.ok.
- Parsing non-JSON responses.
Key takeaways
- SyntaxError: Unexpected token: This SyntaxError says the parser met something it did not expect.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
What does '<' at position 0 mean?
The response body starts with HTML, not JSON.
How do I find which line caused SyntaxError: Unexpected token?
The error message and stack trace show the file and line; start there and work backwards.