es6 Temporal dead zone

- 1 answer

Ad

I know Temporal dead zone in es6. But I was confused about the procedure of the following code.

Javascript is a kind of Interpretive language.How does it knows there is a s will be declared in this block behind rather than use the s outside this block.

In an other word , what is the procedure of the following code ?? I am new here, please help me.

'use strict'
var s = 1;
if (true){
    console.log(s);
    console.log("AAA");
    let s = 2;
}
Ad

Answer

Ad

Javascript code is run in multiple passes. A first pass will go through and deal with all the declarations and assign them to their scopes.

That's how it "knows" that a left hand side reference to "s" will be declared via let for the scope of that if{} block, even though the non-hoisted let hasn't actually declared it yet.

Ad
source: stackoverflow.com
Ad