Skip to content
How to Fix SyntaxError in JavaScript — Unexpected Token, Missing Brackets & JSON Errors

How to Fix SyntaxError in JavaScript — Unexpected Token, Missing Brackets & JSON Errors

DodaTech Updated Jun 15, 2026 3 min read

The Error

SyntaxError: Unexpected token '}'
SyntaxError: Unexpected identifier
SyntaxError: JSON.parse: unexpected character at line 1
SyntaxError: Illegal return statement

A SyntaxError means the JavaScript parser found code that doesn’t follow the language’s grammar rules. Unlike runtime errors that happen during execution, syntax errors prevent the code from running at all.

What Causes It

1. Missing or Extra Brackets/Parentheses

The most common cause — a mismatch in brackets, parentheses, or braces:

// Missing closing parenthesis
function greet(name {
  console.log('Hello');
}

// Extra closing brace
if (true) {
  console.log('ok');
}} // ← extra brace

// Mismatched brackets
const arr = [1, 2, 3); // ← brackets don't match

Fix: Use an editor with bracket matching and indentation guides. Format your code with Prettier.

2. Trailing Commas in JSON

JSON is stricter than JavaScript objects:

// Valid JavaScript, but INVALID JSON
const obj = { name: "Alice", age: 25, };

// JSON.parse will fail
JSON.parse('{"name": "Alice", "age": 25,}');
// SyntaxError: Unexpected token } in JSON at position 24

Fix: Remove trailing commas from JSON strings. Use JSON.stringify() to generate valid JSON rather than writing it by hand.

3. Using Reserved Words

// 'class' is a reserved word (can be used as property name in ES6+)
const obj = { class: 'intro' }; // OK in modern JS

// But you can't use it as a variable
const class = 'intro'; // SyntaxError

Fix: Avoid reserved words as variable names: class, return, if, switch, import, export, etc.

4. Strict Mode Violations

'use strict';
// Assigning to undeclared variable
x = 10; // ReferenceError in strict mode (not SyntaxError)

// But some are SyntaxErrors:
delete Object.prototype; // SyntaxError in strict mode

5. Missing Commas in Object Literals

const obj = {
  name: 'Alice'
  age: 25   // ← Missing comma
};

Fix: Always use commas between object properties. Modern editors highlight missing commas visually.

How to Debug Syntax Errors

1. Read the Error Message Carefully

SyntaxError: Unexpected token '}' (line 42, column 15)

The error tells you the line and column. Check around that position — the actual mistake might be slightly before the reported token.

2. Use a Linter

npx eslint your-file.js

ESLint catches most syntax errors before you even run the code, with precise error messages.

3. Check Braces Incrementally

Comment out half your code and see if the error goes away. Uncomment gradually to find the exact location.

4. Validate JSON Separately

try {
  JSON.parse(malformedJson);
} catch (e) {
  console.error('Invalid JSON:', e.message);
}

Use the browser console’s JSON.parse() or online validators like JSONLint to check your JSON independently.

5. Use a Code Formatter

npx prettier --write your-file.js

Prettier automatically fixes most bracket, parenthesis, and comma issues.

Prevention

  1. Use a linter — ESLint catches syntax errors immediately.
  2. Format on save — enable Prettier or your editor’s built-in formatter.
  3. Never write JSON by hand — use JSON.stringify() or a library.
  4. Use TypeScript — stricter syntax validation catches issues at compile time.
  5. Be careful with minified/concatenated code — missing semicolons between files can cause syntax errors after bundling.

Safe Semicolons (ASI)

JavaScript uses Automatic Semicolon Insertion (ASI), which can cause unexpected syntax:

// Problem: ASI doesn't insert here
const fn = function() {
  return
    { value: 42 }; // Returns undefined, not the object!
};

Fix: Put opening braces on the same line as return:

const fn = function() {
  return {
    value: 42
  };
};

Summary

CauseFix
Missing bracket/parenUse a code formatter
Trailing commas in JSONRemove them or use JSON.stringify()
Reserved wordsRename the variable
Strict mode violationFollow strict mode rules
Missing commasUse linter + formatter

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro