Javascript- Uncaught SyntaxError: Identifier * already declared

console.log(a) //output:ƒ a(){}
    var a = 1;
    function a(){};
    var a = 10;
    console.log(a) //output:10

=====================

var a = 1;
if(true){
function a(){};
var a = 10;
}
console.log(a) // this code throws Uncaught SyntaxError: Identifier 'a' has already been declared

both above the code snippets are the same except for the if.why block makes the last mistake when it is valid in javascript so that delcare the same variable twice in the same area with var, as shown below

 function a(){};
    var a=10; //no error

Also for a slightly different scenario after removing var from `var a = 10 in the above code, then it works fine, but the output is surprising

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

, , 10.. , if, , , javascript var , ... 10? , 10, , .

  var a = 1;
    if(true){
    var a= function(){console.log()}
    a = 10;
    }
    console.log(a) //output:10
+4
3

1

console.log(a) //output:ƒ a(){}
var a = 1;
function a(){};
var a = 10;
console.log(a) //output:10

var a;
a = function(){}; // now a holds the value as a function
console.log(a); // output : f a(){}
a = 1; // a is a var that holds value 1
a = 10; // a is a var that holds value 10
console.log(a); // output : 10

2

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

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);

3

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

var a;
a = 1;
if(true) {
    a = function() {};
    let a;
    a = 10;
}
console.log(a); // output : f a(){}

4

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

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

5

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

var a;
a = 1;
if(true){
    a = function() {};
    let a;
    a = 10;
    console.log(a); // output:10
}
console.log(a); // output : f a(){}
+3

, javascript var , ...

, var a . , ( , ES5).

javascript var,

. function ES6 (, let const), .

+2

Thanks @Bergi for the answer. I have a couple of questions.

(1) the code below causes an error due to the fact that I defined the function if the block or I again declared the same variable a?

 var a = 1;
    if(true){
    function a(){};
    var a = 10;
    }
    console.log(a) // this code throws Uncaught SyntaxError: Identifier 'a' has already been declared

(2) how can we justify the output of the code below, even after I redefined the value 10 .. still shows the output as ƒ a () {}.

var a = 1;
    if(true){
    function a(){};
    a = 10;
console.log(a) //output:10
     }
    console.log(a) //output:ƒ a(){},how????
0
source

Source: https://habr.com/ru/post/1696055/


All Articles