Understanding variable scope(let,var,const),hoisting in Javascript and Temporal Dead Zone(TDZ)
interview questions , Javascript / June 11, 2018

Hoisting in Javascript  To hoist means to pull or move up. Javascript hoisting means variable and function declarations are pulled to the top of the scope while compilation. What actually happens is that declarations are stored in memory. In the following example, we will see how hoisting works.

Here the function test can be used called before it is declared in the code because during the compilation the declaration is hoisted or moved to the top of the scope(current script or current function). Javascript only hoists declarations, not initializations. All declarations (var, let, const, function, class) are hoisted Variable scope in JavaScript Var  The scope of var is the current execution context. That is, the scope of var is global if used outside a function and the scope is the function block while being used inside the function. assigning value to a variable without declaration creates and adds variable to the global scope. This is discouraged and throws an error in strict mode

As you can see the scope of a is the function. Let – block scope The scope of let is the block in which they are defined .let defines block-scoped variables. Let has a scope from the block…

Share this page in social media platforms