Logo
HTMLCSSJavaScriptReactjsnewContactUpdates

Get started today

HTMLCSSJavaScriptReactjsnewContactUpdates

Tools

Resume BuilderQR GeneratorVS Code Editor

Connect

GitHubWhatsApp Channel
global execution context
var let const

Javascript Hoisting and Temporal Dead Zone

By Saket Bhatnagar•June 27, 2025•Beginner to Intermediate

Table of Contents

  1. Hoisting
  2. Temporal Dead Zone (TDZ)

Hoisting

  1. 1Hoisting is the ability of the JavaScript engine to access variables and functions before their declaration line appears in code
  2. 2This doesn’t mean JavaScript moves the code physically upward — instead, it creates memory blocks during the first phase of code execution
  3. 3JavaScript engine executes Code in two phases:
    1. Memory Creation Phase or Variable phase – JS engine scans the whole file and allocates memory for all variables and functions in the same squence as they appear in the code. When all memory is allocated, JS engine moves to execution phase.
    2. Execution Phase – JS executes code line by line and assigns values to variables.
  4. 4What happens in memory phase:
    • Variables declared with var are auto-initialized with undefined
    • Variables declared with let and const are not initialized with any value in memory phase and remain empty until js engine reads the declaration statement in execution phase and initialize it with the value.
    • In case of function, whole function code is stored in memory block. (only for functions those are declared with function keyword)
  5. 5Example: var Hoisting

    var Example

    1console.log(a); // undefined
    2var a = 10;
    💡

    Note

    During memory phase, a is initialized with undefined. So in execution phase, when console.log(a) runs, JS finds that memory block and found value undefined and prints undefined.
  6. 6Example: let Hoisting (TDZ)

    let Example

    1console.log(b); // ReferenceError
    2let b = 10;
    💡

    Note

    In memory phase, b is only allocated memory but not initialized means memory block is empty. So in execution phase,console.log(b) runs, JS finds that memory block and but memory block is empty and that's why it throws error becasue we are trying to access the memory block but nothing is stored there to be printed or logged.

    Understood?. It is the time frame between memory allocation and initialization, so if we try to access memory block or variable before it is initialized, as js engine is not able to find any value to be printed or logged, js engine throws error and this is called Temporal Dead Zone (TDZ).

    Why dead zone? Because we are not able to access the variable before it is initialized.
  7. 7Example: Function Hoisting

    Function Hoisting

    1sayHi(); // Output: Hi
    2
    3function sayHi() {
    4 console.log('Hi');
    5}
    💡

    Note

    In memory phase, sayHi is stored with it's code. So in execution phase, when sayHi() runs, JS finds that memory block and found function code and calls it.

    Function declarations are fully hoisted with their code in memory phase. That’s why you can call sayHi()before its declaration.
  8. 8Interview Key Point: Hoisting = memory is ready before code runs. JS doesn’t move the code; it just prepares memory in advance.

Temporal Dead Zone (TDZ)

  1. 1It is the time frame between memory allocation and initialization, in this time frame we can not access the variable. If we try to access memory block (variable) before it is initialized, as js engine is not able to find any value to be printed or logged, js engine throws error and this is called Temporal Dead Zone (TDZ).
  2. 2Why we call dead zone? Because we are not able to access the variable before it is initialized.
  3. 3

    let (TDZ) Example

    1console.log(b); // ReferenceError
    2let b = 10;
    💡

    Note

    In memory phase, b is only allocated memory but not initialized means memory block is empty. So in execution phase,console.log(b) runs, JS finds that memory block and but memory block is empty and that's why it throws error becasue we are trying to access the memory block but nothing is stored there to be printed or logged.
  4. 4

    const (TDZ) Example

    1console.log(b); // ReferenceError
    2const b = 10;
    💡

    Note

    In memory phase, b is only allocated memory but not initialized means memory block is empty. So in execution phase,console.log(b) runs, JS finds that memory block and but memory block is empty and that's why it throws error becasue we are trying to access the memory block but nothing is stored there to be printed or logged.

Share this article

Last updated: June 27, 2025

Join Our Community

Login to Join

© 2025 Saket Bhatnagar. All rights reserved.