Skip to content

SyntaxError: Unexpected token

Error message

                Unexpected token 'x'
              

Quick summary

JavaScript found a symbol where it shouldn't be — usually a missing bracket, comma, or quote earlier in the code.

Why this happens

  • Missing comma between items in an array or object literal

  • Unclosed string, parenthesis, or bracket from a previous line

  • Extra punctuation or a stray character in the code

Minimal example

✗ Broken code
const arr = [1, 2 3];
✓ Fixed code
const arr = [1, 2, 3];

A comma is missing between 2 and 3 — the parser sees 3 where it expected a comma or closing bracket.

How to diagnose

  • Look at the token mentioned in the error and the line before it

  • Check for missing commas, brackets, or quotes near the reported location

  • Use a linter or formatter to highlight the syntax issue

How to fix

  • Add the missing comma, bracket, or quote

  • Remove any extra or stray punctuation

  • Run the code through a formatter (Prettier) to spot structural issues

How to prevent

  • Use a linter (ESLint) to catch syntax errors as you type

  • Enable editor syntax highlighting and bracket matching

Related Resources

Related Glossary

Related Lessons

Related Practice

Related Tools

← Back to language