JavaScript-Garden icon indicating copy to clipboard operation
JavaScript-Garden copied to clipboard

another curiosity about variable hoisting

Open lewisje opened this issue 9 years ago • 1 comments

The statement var something = something; works even as the first statement in its scope, because it is equivalent to

var something;
something = something;

and it, as imagined, sets something to undefined, even if something was already defined in an outer scope.

To make a local reference to an object in an outer scope with the same name, it needs to be passed in as a function parameter, because otherwise, the inner name will shadow the outer name because of hoisting.

lewisje avatar May 29 '16 20:05 lewisje

Awesome post! Hoisting just make sense for for me now. :-) Thanks for spreading the knowledge!

Cryptoxygen avatar Jan 22 '19 12:01 Cryptoxygen