Solve - Unexpected identifier Error in JavaScript

avatar

Borislav Hadzhiev

Tue Oct 19 20212 min read

Solve - Uncaught SyntaxError Unexpected token #

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 that 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.

This is a good starting point to start debugging.

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.

A good starting point is to start around the line the error occurred and look for:

  • misspelled / incorrect keyword 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 keyword, e.g. Let or Function instead of let and function and correct any typos related to missing or extra comma, colon, parenthesis, quote or brackets.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee