Redeclaring a javascript variable

后端 未结 8 1753
耶瑟儿~
耶瑟儿~ 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:11

    It's nothing more than a reminder that if you do this:

    var x=5;
    var x;
    alert(x);
    

    Result will be 5.

    If you re-declare variable in some other languages for example - result will be undefined, or NaN, but not in javascript.

    0 讨论(0)
  • 2020-11-29 06:17

    It doesn't lose it's value because of Hoisting

    var x = 5;
    var x;
    
    // this is same as
    
    var x; // undefined;
    x = 5;
    

    So when you say "If you redeclare a JavaScript variable, it will not lose its value."

    As per hoisting, the declaration(s), all of them , move to the top. And then the variable is assigned.

    var x = 25;
    var x; // redeclare first time
    var x; // redeclare second time
    
    // is same as 
    
    var x; // undefined
    var x; // Not sure if this happens, but doesn't make a difference, it's still undefined
    x = 25;
    

    As for practicality, it happens sometimes. Look at @steveoliver 's answer.

    0 讨论(0)
  • 2020-11-29 06:17

    Keep in mind that only variables declared with var can be re-declared. If you try to re-declare a variable declared with let or const (which is the ES2015 Javascript syntax that should be used in most cases nowadays), even worse than losing the value, an error will be thrown:

    let foo = 'foo';
    let foo;

    So, in codebases which use modern Javascript syntax, re-declaring a variable simply isn't possible - the interpreter needs to be able to identify a single point in the code after which a let or const variable gets properly initialized. Before that point, the variable name will exist in the temporal dead zone.

    0 讨论(0)
  • 2020-11-29 06:17

    In general, it can be considered bad style to have var assignments after other statements due to the problem of hoisting (see here). Using the "Single var pattern" (see here), redeclarations could only happen like in Steve Oliver's Google Analtyics example. I'd refactor the example above to:

    var x, max = 100; // no further var declarations afterwards!
    
    for (x = 0; x < max; x++) { }
    
    alert(x);
    
    // redeclaration 'var x = "hello"' doesn't make any sense here
    // and would be complained about by JSLint/-Hint 
    x = 'hello';
    alert(x);
    

    A redeclaration can make sense however when using default values for optional parameters (which is what the Google Analytics example is about, I assume):

    function abc(param1) {
      var param1 = param1 || 'default value';
    }
    
    0 讨论(0)
  • 2020-11-29 06:22

    It is pretty simple re-declaring doesn't actually affect anything, you just have to remember that if you reassign value within scope then the reassigned value is limited to scope and outside of scope it will still be globally declared value

    var page =1 ;
    function htmlcode(page) { 
    page = "keka";
    console.log("inside " + page);
    }
    
    htmlcode(page);
    console.log("inside " + page);
    
    Output : inside keka
             inside 1
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题