Skip to main content

How to Resolve "SyntaxError: Unexpected identifier" in JavaScript

The SyntaxError: Unexpected identifier is one of the most common errors in JavaScript. It's a generic error message from the JavaScript parser that means it encountered a word (an "identifier") where it was expecting something else, like a piece of punctuation (a comma, a semicolon, a parenthesis, an operator, etc.).

This guide will explain the root cause of this error and walk you through the most common syntax mistakes that trigger it, including missing commas, incorrect keyword casing, and missing operators.

The Core Problem: What is an "Unexpected Identifier"?

In JavaScript, an identifier is a name for a variable, function, or property (e.g., myVar, calculateTotal, name).

The "Unexpected identifier" error occurs when the JavaScript parser is reading your code and finds an identifier where it syntactically should not be. It's like reading an English sentence and finding a noun where you expect a period or a comma. The parser gets confused and reports the error.

Cause 1 (Most Common): Missing Commas in Object or Array Literals

This is the most frequent cause. You forget to put a comma between properties in an object or elements in an array.

Example of problem:

// Problem: Missing a comma between the `name` and `age` properties.
const user = {
name: 'Alice',
age: 30 // ⛔️ The parser sees the identifier `age` right after the value 'Alice'
// where it expected a comma or a closing brace.
};
// SyntaxError: Unexpected identifier 'age'

The same happens in arrays:

const items = ['apple' 'banana']; // ⛔️ SyntaxError: Unexpected identifier 'banana'

Solution: always separate properties in an object literal and elements in an array literal with a comma.

// ✅ Correct: Comma added between properties.
const user = {
name: 'Alice',
age: 30,
};

// ✅ Correct: Comma added between elements.
const items = ['apple', 'banana'];
note

Best Practice: Using a "trailing comma" (a comma after the last property or element) is valid in modern JavaScript and can help prevent this error when adding new items.

Cause 2: Incorrect Keyword Casing

JavaScript keywords (let, const, class, function, etc.) are case-sensitive and must be written in lowercase. If you capitalize them, the parser treats them as variable names (identifiers), not keywords.

Example of problem:

// Problem: 'Const' is not the `const` keyword. It's an identifier.
Const myVar = 123; // ⛔️ SyntaxError: Unexpected identifier 'myVar'

Why this happens: The parser sees the identifier Const, then a space, then another identifier myVar. It was expecting an assignment operator (=) after the first variable name, so myVar is "unexpected."

Solution: always use lowercase for JavaScript keywords.

// ✅ Correct: Use the lowercase `const` keyword.
const myVar = 123;

// ✅ Correct
let anotherVar = 456;
class MyClass {}
function myFunction() {}

Cause 3: Missing Operators in Expressions

This error can also occur if you try to concatenate strings or perform other operations without the correct operator.

Example of problem:

// Problem: Missing the `+` operator to concatenate the strings.
const name = 'Alice';
const message = 'Hello, ' name; // ⛔️ SyntaxError: Unexpected identifier 'name'

The parser sees the string 'Hello, ' and then immediately sees the identifier name. It was expecting a semicolon, a closing parenthesis, or an operator like +, so name is "unexpected."

Solution: use the correct operator (+) for concatenation, or use a modern template literal.

// ✅ Correct: Using the `+` operator.
const message = 'Hello, ' + name;

// ✅ Correct (Modern): Using a template literal.
const message2 = `Hello, ${name}`;

How to Debug the Error

This is a SyntaxError, which means your browser or Node.js environment will tell you exactly where the problem is.

  1. Check the Console: The error message in your browser's developer console or your terminal will include the filename and the line number where the parser got confused.
  2. Use a Code Editor/Linter: Modern code editors (like VS Code) and linters (like ESLint) will highlight syntax errors directly as you type, often with a red squiggly line, allowing you to fix them before you even run the code.

Conclusion

The SyntaxError: Unexpected identifier is almost always a simple typo in your code that violates JavaScript's syntax rules.

When you see this error, check the reported line for:

  • A missing comma (,) in an object or array.
  • An incorrectly capitalized keyword (like Const instead of const).
  • A missing operator (like + for string concatenation).