Skip to content

How to Fix SyntaxError: Unexpected token in JavaScript

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix SyntaxError: Unexpected token in JavaScript. We cover key concepts, practical examples, and best practices.

The Problem

JavaScript throws SyntaxError: Unexpected token when the parser encounters a character it does not expect at that position, usually from missing brackets, mismatched quotes, trailing commas, or stray operators.

Quick Fix

Step 1: Check for mismatched brackets

A missing closing brace causes the parser to misinterpret the next character:

const obj = { name: 'Alice'; };
SyntaxError: Unexpected token ';'

Replace the semicolon inside the object with a comma:

const obj = { name: 'Alice' };
console.log(obj);
{ name: 'Alice' }

Step 2: Fix missing closing parentheses

Each opening parenthesis must have a matching closing one:

console.log('hello';
SyntaxError: Unexpected token ';'

Add the missing closing parenthesis:

console.log('hello');
hello

Step 3: Remove trailing commas in JSON

JSON does not allow trailing commas after the last property:

const data = '{"name": "Alice", "age": 30,}';
JSON.parse(data);
SyntaxError: Unexpected token '}'

Remove the trailing comma:

const data = '{"name": "Alice", "age": 30}';
JSON.parse(data);
{ name: 'Alice', age: 30 }

Step 4: Check for stray operators

An operator without operands causes syntax errors:

const x = 5 +
const y = 3;
SyntaxError: Unexpected token 'const'

Place the operator on the same line as its operands:

const x = 5 + 3;
console.log(x);
8

Prevention

  • Use a code editor with bracket matching and syntax highlighting.
  • Run npx eslint --fix before committing code.
  • Validate JSON with jq . file.json before parsing in code.
  • Use Prettier for automatic formatting on save.

Common Mistakes with syntaxerror

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world JS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does "Unexpected token" sometimes point to a valid character?

When the parser is confused by an earlier error, the reported location may be misleading. For example, a missing opening brace can cause a later closing brace to be flagged as "unexpected." Always look at the context around the reported line, not just the token itself.

How do I find the real cause when the error line is not helpful?

Narrow down the problem by removing half the code until the error disappears (binary search debugging). Comment out blocks, reload, and repeat. This isolates the offending section much faster than reading every line.

Can I use a linter to catch syntax errors before runtime?

Yes. ESLint catches syntax errors during development, not just during execution. Run npx eslint yourfile.js to check for issues. Many editors integrate ESLint to show errors inline as you type.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro