close

[Solved] Uncaught SyntaxError: Identifier ‘a’ has already been declared

Hello Guys, How are you all? Hope You all Are Fine. Today I am running simple code but I am facing the following error Uncaught SyntaxError: Identifier ‘a’ has already been declared in Javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Uncaught SyntaxError: Identifier ‘a’ has already been declared Error Occurs ?

I am running simple code But I am facing the following error.

Uncaught SyntaxError: Identifier 'a' has already been declared

Here is my simple Code.

var a = 1;
if(true){
  function a(){};
  var a = 10;
}
console.log(a)

How To Solve Uncaught SyntaxError: Identifier ‘a’ has already been declared Error ?

  1. How To Solve Uncaught SyntaxError: Identifier ‘a’ has already been declared Error?

    To Solve Uncaught SyntaxError: Identifier ‘a’ has already been declared Error Here you didn’t use var for the declaration of a in the block scope. You used a function declaration, which does respect block scopes (otherwise it would be completely invalid code, as in ES5 strict mode). The same applies here. The function declaration in the block uses ES6 declaration semantics (like let or const), which does not allow redeclarations.

  2. Uncaught SyntaxError: Identifier ‘a’ has already been declared

    To Solve Uncaught SyntaxError: Identifier ‘a’ has already been declared Error Here you didn’t use var for the declaration of a in the block scope. You used a function declaration, which does respect block scopes (otherwise it would be completely invalid code, as in ES5 strict mode). The same applies here. The function declaration in the block uses ES6 declaration semantics (like let or const), which does not allow redeclarations.

Solution 1

Here you didn’t use var for the declaration of a in the block scope. You used a function declaration, which does respect block scopes (otherwise it would be completely invalid code, as in ES5 strict mode). The same applies here. The function declaration in the block uses ES6 declaration semantics (like let or const), which does not allow redeclarations.

Solution 2

You can use as given below.

var a;
a = 1;
if(true) {
    a = function() {};
    let a; // The function declaration in the block uses ES6 declaration semantics (like let or const), which does not allow re-declarations.
    var a; // throws Uncaught SyntaxError: Identifier 'a' has already been declared
    a = 10;
}
console.log(a);

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?

Also Read

Leave a Comment