Declaring a Javascript variable twice in same scope - Is it an issue?

前端 未结 3 767
甜味超标
甜味超标 2020-12-14 14:59

Would the following code cause any issues?:

var a = 1;
var a = 2;

My understanding is that javascript variables are declared at the start o

3条回答
  •  情话喂你
    2020-12-14 15:52

    Declaring the same variable twice is as good as declaring it once. Below statement will not bring any impact.

    var a, a;
    

    in the below case you are just overriding the variable foo. This will have an impact if you have foo defined in the local and global scope. JS will search foo in the local scope first and then if it doesn't find it will look in the global scope.

    var foo;
    var bar;
    foo = 'a';
    foo = 'b';
    bar = 'c';
    

提交回复
热议问题