VARIABLE HOISTING
Variable Hoisting Variable Hoisting is a behaviour in Javascript where variable declaration are moved to the top of the scope before execution function hoist() { a = 20; var b = 100; } hoist(); console.log(a); // Accessible as a global variable outside hoist() function. Output: 20 console.log(b); // Since it was declared, it is confined to the hoist() function scope. We can't print it out outside the confines of the hoist() function. Output: ReferenceError: b is not defined
Hoisting and var The var variable is scoped to the function it sits within, or the global scope if it isn’t within a local function. Global Variable, console.log(hoist); // Output: undefined var hoist = 'The variable has been hoisted.'; Function Variable, function hoist() { console.log(message); var message='Hoisting is all the rage!' } hoist(); // Output: undefined
Hoisting and let The let variable is a new form of variable introduced in ES2015. Instead of being scoped to the nearest function, let is scoped to the nearest block. console.log(hoist); // Output: ReferenceError: hoist is not defined let hoist = 'The variable has been hoisted.'; Declare the variables first, let hoist; console.log(hoist); // Output: undefined hoist = 'Hoisted'
Hoisting and const The const keyword was introduced in es6 to allow immutable variables. That is, variables whose value cannot be modified once assigned. console.log(hoist); // Output: ReferenceError: hoist is not defined let hoist = 'The variable has been hoisted.'; Globally, const PI; console.log(PI); // Output: SyntaxError: Missing initializer in const declaration PI=3.142;
So are let and const variables not hoisted? All declarations are hoisted in javascript, while the var declarations are initialized with undefined, but let and const declarations remain uninitialized. ● They will be only initialized when their lexical block is evaluated by engine. ● Making it inaccessible before it is evaluated. ● This inaccessible time span between creation and initialization is called “Temporal Dead zone”. So, answer to the question, let and const hoist but cannot access them before the actual declaration is evaluated

Variable hoisting in JavaScript

  • 1.
  • 2.
    Variable Hoisting Variable Hoistingis a behaviour in Javascript where variable declaration are moved to the top of the scope before execution function hoist() { a = 20; var b = 100; } hoist(); console.log(a); // Accessible as a global variable outside hoist() function. Output: 20 console.log(b); // Since it was declared, it is confined to the hoist() function scope. We can't print it out outside the confines of the hoist() function. Output: ReferenceError: b is not defined
  • 3.
    Hoisting and var Thevar variable is scoped to the function it sits within, or the global scope if it isn’t within a local function. Global Variable, console.log(hoist); // Output: undefined var hoist = 'The variable has been hoisted.'; Function Variable, function hoist() { console.log(message); var message='Hoisting is all the rage!' } hoist(); // Output: undefined
  • 4.
    Hoisting and let Thelet variable is a new form of variable introduced in ES2015. Instead of being scoped to the nearest function, let is scoped to the nearest block. console.log(hoist); // Output: ReferenceError: hoist is not defined let hoist = 'The variable has been hoisted.'; Declare the variables first, let hoist; console.log(hoist); // Output: undefined hoist = 'Hoisted'
  • 5.
    Hoisting and const Theconst keyword was introduced in es6 to allow immutable variables. That is, variables whose value cannot be modified once assigned. console.log(hoist); // Output: ReferenceError: hoist is not defined let hoist = 'The variable has been hoisted.'; Globally, const PI; console.log(PI); // Output: SyntaxError: Missing initializer in const declaration PI=3.142;
  • 6.
    So are letand const variables not hoisted? All declarations are hoisted in javascript, while the var declarations are initialized with undefined, but let and const declarations remain uninitialized. ● They will be only initialized when their lexical block is evaluated by engine. ● Making it inaccessible before it is evaluated. ● This inaccessible time span between creation and initialization is called “Temporal Dead zone”. So, answer to the question, let and const hoist but cannot access them before the actual declaration is evaluated