0

Why, with this code, the first console.log() generates an error (surprise, because I thought this behavior was only with var, not let):

(function() {
    "use strict";
    let i=12;
    function test() {  
        console.log(i);   
        let i=4544444;     
        console.log(i);     
    };
    test();
})();

do I get:

Uncaught ReferenceError: i is not defined

? Moreover, if I comment those lines:

(function() {
    "use strict";
    let i=12;
    function test() {  
        console.log(i);   
        //let i=4544444;     
        //console.log(i);     
    };
    test();
})();

Then it works (I thought with "use strict" you cant have variables with the same name at the same time)

Olivier Pons
  • 13,972
  • 24
  • 98
  • 190
  • You used `i` in a **temporal zone**. i has local scope in the `test` method since it hoisted from `let i=4544444;` but not initialized yet. – gurvinder372 Feb 26 '18 at 08:53
  • `I thought with "use strict" you cant have variables with the same name at the same time` not exactly sure what you mean, but, read what strict mode is https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode – Jaromanda X Feb 26 '18 at 08:54
  • `let` isn't hoisted @gurvinder372 - if it were, then there would be no error – Jaromanda X Feb 26 '18 at 08:55
  • @gurvinder372 I thought this behavior was *only* with `var`, not `let` – Olivier Pons Feb 26 '18 at 08:56
  • @gurvinder372 is on the right track though - [Another example of temporal dead zone combined with lexical scoping](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Another_example_of_temporal_dead_zone_combined_with_lexical_scoping) – Jaromanda X Feb 26 '18 at 08:57
  • Possible dupe of https://stackoverflow.com/questions/33198849/what-is-the-temporal-dead-zone – gurvinder372 Feb 26 '18 at 08:58
  • Those are 2 separate `i` because they are in different scopes. – zer00ne Feb 26 '18 at 08:58
  • @zer00ne Then there should not be error, and especially "not defined". Maybe "already define" the line after (I would understand this behavior, more logical to me) – Olivier Pons Feb 26 '18 at 09:02
  • @OlivierPons I was referring to this statement: *"...you cant have variables with the same name at the same time"* As for the reason for the error, gurvinder372 and Jaromanda X are correct about tdz. – zer00ne Feb 26 '18 at 09:38

2 Answers2

1

let variables are registered at the top of the block. But when the variable is accessed before declaration, JavaScript throws an error: ReferenceError: is not defined. From the declaration statement up to the beginning of the block the variable is in a temporal dead zone and cannot be accessed.

function isTruthy(value) {  
  var myVariable = 'Value 1';
  if (value) {
    /**
     * temporal dead zone for myVariable
     */
    // Throws ReferenceError: myVariable is not defined
    console.log(myVariable);
    let myVariable = 'Value 2';
    // end of temporary dead zone for myVariable
    console.log(myVariable); // => 'Value 2'
    return true;
  }
  return false;
}
isTruthy(1); // => true 

Source: https://dmitripavlutin.com/javascript-hoisting-in-details/

zelda11
  • 383
  • 1
  • 7
0

let keyword has limited scope to the block, statement, or expression on which it is used. As in your case First let i = 12 has scope of entire block but second let i = 4544444 has scope of test function block so in this block before declare i, you are using i in console.log, so this generating Uncaught ReferenceError: i is not defined and when you comment the let i = 4544444 then i of value 12 is used

reference to let keyword scope

Arvind Pal
  • 73
  • 9