Skip to content

Unexpected Token

Quick summary

The parser encountered a symbol or keyword where it did not belong — usually a missing bracket, comma, or semicolon.

Why this happens

  • Missing comma between items in an array or object

  • Missing closing bracket, brace, or parenthesis

  • Extra or misplaced punctuation

Minimal example

✗ Broken code
const arr = [1, 2 3]; // Missing comma after 2
console.log(arr);
✓ Fixed code
const arr = [1, 2, 3]; // Comma added
console.log(arr);

The parser sees 2 3 and does not know what to do with the 3 — it expected a comma or closing bracket. Adding the comma fixes it.

How to diagnose

  • Read the error message — it names the unexpected token and its position

  • Check the line mentioned and the line before it

  • Use bracket matching in your editor to find unmatched pairs

How to fix

  • Add the missing comma, bracket, or semicolon

  • Remove the extra punctuation

  • Run a formatter/linter to catch syntax issues automatically

How to prevent

  • Use a code editor with syntax highlighting and bracket matching

  • Run a linter that catches syntax errors as you type

Related Resources

Related Glossary

Related Lessons

  • Errors

    Learn about common syntax errors.

← Back to language