How to Fix JavaScript SyntaxError: Unexpected token
In this tutorial, you'll learn about How to Fix JavaScript SyntaxError: Unexpected token. 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. This is almost always caused by missing closing brackets, mismatched quotes, trailing commas in JSON, or stray operators.
Quick Fix
Step 1: Check for mismatched brackets
Count opening and closing braces, parentheses, and brackets:
node -e "const obj = { name: 'Alice'; }"
SyntaxError: Unexpected token ';'
The semicolon inside the object literal should be a comma:
node -e "const obj = { name: 'Alice' }; console.log(obj);"
{ name: 'Alice' }
Step 2: Fix missing closing parenthesis
A common mistake is forgetting to close a function call:
node -e "console.log('hello';"
SyntaxError: Unexpected token ';'
Add the missing closing parenthesis:
node -e "console.log('hello');"
hello
Step 3: Validate JSON with jq
If the unexpected token error comes from JSON parsing, use jq to validate:
echo '{"name": "Alice", "age": 30,}' | node -e "process.stdin.on('data', d => console.log(JSON.parse(d)))"
SyntaxError: Unexpected token '}'
Remove the trailing comma after the last property.
Step 4: Check for stray operators
A dangling operator on its own line causes an unexpected token error:
node -e "
const x = 5
+
const y = 3
"
Fix by placing the operator on the correct line:
node -e "
const x = 5 + 3
console.log(x);
"
8
Alternative Solutions
Use a linter with automatic fixing
ESLint can catch and fix syntax errors automatically:
npx eslint --fix script.js
Validate JSON with Python
Use Python for JSON validation if jq is not available:
python3 -m json.tool data.json
Common Mistakes to Avoid
Leaving trailing commas in JSON. JavaScript allows trailing commas in objects and arrays, but JSON does not. Use JSON.parse() after removing trailing commas.
Missing a comma between object properties. Each property in an object literal must be separated by a comma except the last one.
Unclosed template literals. A backtick that is not closed causes an unexpected token error that can span many lines. Match every opening backtick.
Pro Tips
Use Prettier for automatic formatting. Prettier reformats code on save, catching missing brackets, trailing commas, and indentation issues.
Run npx eslint before committing. Add a pre-commit hook that runs ESLint to catch syntax errors before they reach the repository.
Use a linter with --fix for quick corrections. ESLint's --fix flag automatically corrects many syntax issues: npx eslint --fix script.js.
Use a JSON validator extension in your editor. VS Code extensions like JSON Tools format and validate JSON files on save, catching syntax errors before they reach your JavaScript code.
Prevention
- Use a code editor with bracket matching and syntax highlighting.
- Run
npx eslint .to catch syntax errors before execution. - Lint your JSON files with
jq . file.jsonbefore parsing them in code.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro