Redeclaring a javascript variable

后端 未结 8 1771
耶瑟儿~
耶瑟儿~ 2020-11-29 05:33

In this tutorial there is written:

If you redeclare a JavaScript variable, it will not lose its value.

Why should I redeclare a variable? Is it

8条回答
  •  抹茶落季
    2020-11-29 06:27

    In javascript there is no block scope so it is advisable to redeclare a variable for clarification purposes; this makes for better code.

    For example:

    for (var x=0; x< 100; x++) { }
    
    alert(x); //In most languages, x would be out of scope here.
              //In javascript, x is still in scope.
    
    
    //redeclaring a variable helps with clarification: 
    var x = "hello";
    alert(x);
    

提交回复
热议问题