Unexpected identifier Error in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Uncaught SyntaxError Unexpected identifier #

The "Uncaught SyntaxError: Unexpected identifier" error occurs for 2 main reasons:

  1. Misspelling a keyword, e.g. Let or Class instead of let and class.
  2. Having a typo in your code, e.g. a missing or extra comma, parenthesis, quote or bracket.

uncaught syntaxerror unexpected identifier

Note the text to the right of the error message - index.js:4. This means that the error occurred in the index.js file on line 4.

Here are some common examples of when the "Uncaught SyntaxError: Unexpected identifier" occurs.

index.js
// ⛔️ Uncaught SyntaxError: Unexpected identifier Let age = 30; // 👈️ should be let // ⛔️ Uncaught SyntaxError: Unexpected identifier Class Person { // 👈️ should be class } // ⛔️ Uncaught SyntaxError: Unexpected identifier Function sum(a,b) { // 👈️ should be function return a + b; } // ⛔️ Uncaught SyntaxError: Unexpected identifier const obj = { first: 'James' // 👈️ missing comma last: 'Doe', };

The first and second examples show how misspelling a keyword causes the error. Keywords are case sensitive.

You can paste your code into an online Syntax Validator. The validator should be able to tell you on which line the error occurs.

You can also hover over the squiggly red line to get additional information.

Alternatively, use your browser's Console tab to see on which line the error was thrown.

Start looking for the cause around the line where the error occurred and look for:

  • misspelled or incorrect keywords like Let, Const, Class or Function
  • a missing colon, comma, bracket, parenthesis, quote
  • an extra colon, comma, bracket, parenthesis, quote

Conclusion #

To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function, and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.