SyntaxError: Unexpected token '}'
SyntaxError: Unexpected token '<'
SyntaxError: Unexpected token 'export'
JavaScript encountered a character it didnβt expect. The specific token tells you what went wrong.
Unexpected token } or ) β mismatched brackets
// β Extra closing bracket
function greet(name) {
if (name) {
console.log(name);
}} // Extra }
}
// β
Match your brackets
function greet(name) {
if (name) {
console.log(name);
}
}
Use your editorβs bracket matching feature (VS Code highlights matching brackets).
Unexpected token < β HTML instead of JS
Your server is returning an HTML page (like a 404) instead of JavaScript.
// β The API returned HTML (error page) instead of JSON
const data = await response.json();
// SyntaxError: Unexpected token '<' (the HTML starts with <html>)
// β
Check the response first
const response = await fetch('/api/data');
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
Also check that your script src paths are correct in HTML.
Unexpected token export β module system mismatch
// β Using ES modules in a CommonJS file
export default function greet() {}
// SyntaxError: Unexpected token 'export'
Fixes:
- Add
"type": "module"topackage.json - Or rename the file to
.mjs - Or use
module.exportsinstead ofexport
Unexpected token in JSON
// β Trailing comma in JSON
{ "name": "Alice", } // SyntaxError!
// β Single quotes in JSON
{ 'name': 'Alice' } // SyntaxError!
// β
Valid JSON
{ "name": "Alice" }
See also: Unexpected token in JSON fix for the full guide.
Quick debugging
The error message includes a line number. Go to that line and look for:
- Missing or extra brackets
{},(),[] - Missing commas or semicolons
- Wrong quotes (single vs. double in JSON)
- Copy-pasted invisible characters (retype the line)
See also: ESLint parsing error fix
Related: JavaScript Array Methods Cheat Sheet