Tutorials Logic, IN info@tutorialslogic.com

SyntaxError Unexpected token: Causes, Fixes, Examples & Interview Tips

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.

Common Causes

  • Missing or extra brackets, parentheses, or braces
  • Missing commas in objects or arrays
  • Missing semicolons (in some cases)
  • Using reserved keywords incorrectly
  • Invalid JSON or object syntax

Quick Fix (TL;DR)

Quick Solution

Quick Solution
// ❌ 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

Forgetting to close brackets, braces, or parentheses.

Forgetting commas between object properties or array elements.

Using JavaScript reserved keywords as variable names or in wrong context.

Incorrect arrow function syntax.

Incorrect template literal syntax.

Problem

Problem
// 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 '{'

Solution

Solution
// 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

Problem

Problem
// 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'
];

Solution

Solution
// Add commas in object
const user = {
    name: 'John',  // ✅
    age: 30
};

// Add commas in array
const colors = [
    'red',   // ✅
    'blue',  // ✅
    'green'
];

Problem

Problem
// 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
}

Solution

Solution
// 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;
}

Problem

Problem
// 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

Solution

Solution
// 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; // ✅

Problem

Problem
// 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

Solution

Solution
// 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

  • Use a good IDE - Modern editors highlight syntax errors
  • Enable ESLint - Catches syntax errors before running code
  • Use Prettier - Auto-formats code and fixes common issues
  • Check bracket matching - Ensure all brackets are closed
  • Read error messages carefully - They point to the exact location
  • Use online validators - Test code in JSFiddle or CodePen
  • Learn syntax rules - Understand JavaScript syntax fundamentals

Related Errors

Frequently Asked Questions

This error occurs when JavaScript encounters code that doesn't follow syntax rules. Common causes include missing brackets/braces, missing commas, using reserved keywords incorrectly, or invalid arrow function syntax.

Read the error message to find the exact location, check for missing brackets or commas, ensure proper syntax for arrow functions and template literals, and use an IDE with syntax highlighting.

This usually means there's a missing closing parenthesis before the brace, or incorrect syntax in the previous line. Check if statements, function calls, and arrow functions for proper syntax.

Use a modern IDE with syntax highlighting, enable ESLint for real-time error detection, use Prettier for auto-formatting, and learn JavaScript syntax fundamentals.

Reserved keywords include: class, function, const, let, var, if, else, for, while, return, await, async, etc. These cannot be used as variable names.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.