Ad Space — Top Banner

Unexpected token

JavaScript Programming Language

Severity: Critical

What Does This Error Mean?

An 'Unexpected token' error means JavaScript found a character or symbol it did not expect at that point in the code. Think of it like a grammar checker that found a word in the wrong place. JavaScript cannot continue reading your code because the structure does not make sense from that point on.

Affected Models

  • All browsers
  • Node.js
  • Deno
  • Any JavaScript runtime

Common Causes

  • A missing or extra comma, bracket, or parenthesis in the code
  • Using a newer JavaScript feature in an older browser that does not support it
  • Trying to parse invalid JSON — a missing quote or trailing comma in the JSON data
  • A typo that puts a symbol in the wrong place
  • Mixing up arrow function syntax or other shorthand incorrectly

How to Fix It

  1. Open your browser console (F12 > Console). The error will show the file name and line number. Go to that exact line.

    The arrow or caret (^) in the error message sometimes points to the exact character JavaScript did not expect.

  2. Look at the characters around the reported line. Check for a missing or extra comma, bracket, or parenthesis.

    Even one extra or missing character can cause this error. Compare your code carefully against a working example.

  3. If the error appears when parsing JSON data (from an API or a file), validate the JSON at jsonlint.com. Paste the JSON and it will highlight exactly what is wrong.

    Common JSON mistakes: trailing commas after the last item, single quotes instead of double quotes, or unquoted property names.

  4. If the error mentions a feature like async/await, arrow functions, or template literals, check if your browser supports that JavaScript version.

    Very old browsers may not support modern JavaScript syntax. Consider using a tool like Babel to convert modern code to older syntax.

  5. Use a code editor with syntax highlighting (like VS Code). It will color-code your code and make structural mistakes much easier to spot.

    Mismatched brackets often become obvious when you can see the color-coding suddenly shift unexpectedly.

When to Call a Professional

Unexpected token errors are always fixable by the developer. They indicate a writing mistake in the code or data. No professional help is needed — just careful examination of the line indicated in the error.

Frequently Asked Questions

What is a 'token' in JavaScript?

A token is the smallest meaningful unit in code — like a word or punctuation mark in a sentence. Tokens include keywords (if, let, function), operators (+, =, ===), brackets, numbers, and strings. When JavaScript says it found an 'unexpected token', it means a symbol or word appeared somewhere the language rules do not allow it.

Why does an error on one line actually come from a problem on a different line?

JavaScript reads your code from top to bottom. A missing closing bracket on line 5 might not cause confusion until line 20 when something else appears. Always check several lines above the reported line number when looking for the actual mistake.

Can 'Unexpected token' happen with JSON data?

Yes — this is very common. When you use JSON.parse() and the string is not valid JSON, you get an 'Unexpected token' error. Common JSON mistakes include trailing commas, single quotes, and missing quotes around property names. Use a JSON validator like jsonlint.com to find the exact problem in your JSON data.