SyntaxError: Unexpected identifier in JavaScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
3 min

banner

# SyntaxError: Unexpected identifier in JavaScript [Solved]

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

The error message shows on which line the error occurred. The screenshot above shows that the error occurred in the index.js file on line 4.

Here are some common examples of how the error occurs.

index.js
// โ›”๏ธ Uncaught SyntaxError: Unexpected identifier Let age = 30; // ๐Ÿ‘ˆ๏ธ should be let // โ›”๏ธ Uncaught SyntaxError Class Person { // ๐Ÿ‘ˆ๏ธ should be class } // โ›”๏ธ Uncaught SyntaxError Function sum(a,b) { // ๐Ÿ‘ˆ๏ธ should be function return a + b; } // โ›”๏ธ Uncaught SyntaxError const obj = { first: 'Bobby' // ๐Ÿ‘ˆ๏ธ missing comma last: 'Hadz', }; Var example = 'hello' // ๐Ÿ‘ˆ๏ธ should be var

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

Here is how to resolve the error in these particular cases.

index.js
let age = 30; //๏ธ โœ… let (lowercase l) class Person { // โœ… class (lowercase c) } function sum(a,b) { // โœ… function (lowercase f) return a + b; } const obj = { first: 'Bobby', // โœ… added missing comma last: 'Hadz', }; var example = 'hello' // โœ… var (lowercase v)

resolve the error by using correct casing

The code for this article is available on GitHub
If you don't see where the error occurred, press F12 to open your browser's developer tools and click on the Console tab.

The error message should display the name of the file and the line on which the error occurred.

# The most common causes of the error

The most common causes of the error are:

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

The syntax of a for loop can be quite tricky, so make sure you don't have any syntactical errors if you use any for loop.

index.js
const arr = ['bobby', 'hadz', 'com']; for (let index = 0; index < arr.length; index++) { // โœ… bobby, hadz, com console.log(arr[index]); }

use correct syntax in for loops

The code for this article is available on GitHub

# Having a missing comma

Make sure you don't have any missing commas between the properties of an object or an array's elements.

index.js
const obj = { first: 'Bobby' // ๐Ÿ‘ˆ๏ธ missing comma last: 'Hadz', }; const arr = ['bobby' 'hadz', 'com'] // ๐Ÿ‘ˆ๏ธ missing comma

To solve the error, separate the key-value pairs of an object or the array's element by commas.

index.js
const obj = { first: 'Bobby', last: 'Hadz', }; const arr = ['bobby', 'hadz', 'com'];
The code for this article is available on GitHub

When working with objects, it is recommended to add a trailing comma after the last key-value pair.

index.js
const obj = { name: 'bobby', id: 5, age: 30, // ๐Ÿ‘ˆ๏ธ add trailing comma };

This way you don't have to remember to add a comma before adding the next key-value pair to the object.

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

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

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

Copyright ยฉ 2024 Borislav Hadzhiev