SyntaxError: Unexpected token - How to Fix (2026)
What is SyntaxError: Unexpected Token?
The error SyntaxError: Unexpected token occurs when JavaScript encounters code that doesn't follow the language syntax rules. This is a parse-time error, meaning the code won't even run.
Error Message:
SyntaxError: Unexpected token '{'SyntaxError: Unexpected token, expected ","
Common Causes
Quick Fix (TL;DR)
// ❌ Problem - Missing closing brace
function greet() {
console.log('Hello');
// SyntaxError: Unexpected end of input
// ✅ Solution - Add closing brace
function greet() {
console.log('Hello');
}
// ❌ Problem - Missing comma
const user = {
name: 'John'
age: 30 // Missing comma
};
// ✅ Solution - Add comma
const user = {
name: 'John',
age: 30
};
Common Scenarios & Solutions
Scenario 1: Missing Closing Brackets
Forgetting to close brackets, braces, or parentheses.
// Missing closing brace
function calculate(a, b) {
return a + b;
// SyntaxError: Unexpected end of input
// Missing closing bracket
const numbers = [1, 2, 3, 4;
// SyntaxError: Unexpected token ';'
// Missing closing parenthesis
if (x > 10 {
console.log('Greater');
}
// SyntaxError: Unexpected token '{'
// Add closing brace
function calculate(a, b) {
return a + b;
} // ✅
// Add closing bracket
const numbers = [1, 2, 3, 4]; // ✅
// Add closing parenthesis
if (x > 10) { // ✅
console.log('Greater');
}
// Use IDE with bracket matching
// Most editors highlight matching brackets
Scenario 2: Missing Commas in Objects/Arrays
Forgetting commas between object properties or array elements.
// Missing comma in object
const user = {
name: 'John'
age: 30 // SyntaxError: Unexpected identifier
};
// Missing comma in array
const colors = [
'red'
'blue' // SyntaxError: Unexpected string
'green'
];
// Add commas in object
const user = {
name: 'John', // ✅
age: 30
};
// Add commas in array
const colors = [
'red', // ✅
'blue', // ✅
'green'
];
Scenario 3: Using Reserved Keywords
Using JavaScript reserved keywords as variable names or in wrong context.
// Using reserved keyword as variable
const class = 'Math'; // SyntaxError: Unexpected token 'class'
const function = 'test'; // SyntaxError: Unexpected token 'function'
// Using await outside async function
function getData() {
const data = await fetch('/api'); // SyntaxError in non-async function
}
// Use different variable names
const className = 'Math'; // ✅
const functionName = 'test'; // ✅
// Use async function for await
async function getData() {
const data = await fetch('/api'); // ✅
return data;
}
Scenario 4: Invalid Arrow Function Syntax
Incorrect arrow function syntax.
// Missing arrow
const add = (a, b) { return a + b; }; // SyntaxError
// Wrong arrow syntax
const multiply = (a, b) > a * b; // SyntaxError
// Missing parentheses for multiple parameters
const divide = a, b => a / b; // SyntaxError
// Add arrow =>
const add = (a, b) => { return a + b; }; // ✅
// Or implicit return
const add = (a, b) => a + b; // ✅
// Correct arrow syntax
const multiply = (a, b) => a * b; // ✅
// Add parentheses for multiple parameters
const divide = (a, b) => a / b; // ✅
// Single parameter doesn't need parentheses
const square = x => x * x; // ✅
Scenario 5: Template Literal Errors
Incorrect template literal syntax.
// Using regular quotes instead of backticks
const name = 'John';
const message = 'Hello ${name}'; // Doesn't interpolate
console.log(message); // "Hello ${name}"
// Missing closing backtick
const text = `Hello
World; // SyntaxError: Unexpected token
// Wrong interpolation syntax
const greeting = `Hello $name`; // Doesn't interpolate
// Use backticks for template literals
const name = 'John';
const message = `Hello ${name}`; // ✅
console.log(message); // "Hello John"
// Close backtick properly
const text = `Hello
World`; // ✅
// Use ${} for interpolation
const greeting = `Hello ${name}`; // ✅
Best Practices
Related Errors
Key Takeaways
- SyntaxError occurs when code violates JavaScript syntax rules
- Common causes: missing brackets, commas, or incorrect syntax
- Error message shows the exact location of the problem
- Use IDE with syntax highlighting to catch errors early
- ESLint and Prettier help prevent syntax errors
- Template literals require backticks, not regular quotes
Frequently Asked Questions
Level Up Your Javascript Skills
Master Javascript with these hand-picked resources
10,000+ learners
Free forever
Updated 2026
Related JavaScript Topics