javascript - Issue with let keyword -


i playing code , ran situation couldn't identify why 'let' behaving way does.

for below block of code:

var x = 20; // global scope    function f() {    let x = x || 30;  }    f(); // vm3426:1 uncaught referenceerror: x not defined(…)

i error 'x not defined' on executing f(). understand 'let' variables not hoisted since 'x' has global copy, why isn't line inside function 'f' defaulting global copy instead of throwing error? 'let' set variable undeclared (instead of 'undefined' var because of hoisting) @ beginning of function? there way global copy of 'x' within function?

the exception right side x - when initializing block-scope x variable global 1 "forgotten" new 1 still not being declared , cannot used during initialization

compare explicit calling global one

    function f() {       let x = window.x || 30;     } 

also take @ this mdn article let dead zones


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -