Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

JavaScript Hoisting Explained

Understand how JavaScript hoists variable and function declarations.

jiocoder TeamUpdated Jun 6, 20261 min readJavaScript
JavaScript Hoisting Explained

What is Hoisting?#

Hoisting is JavaScript's behavior of moving declarations to the top of their scope before code execution.

Variable Hoisting#

Code
// var is hoisted and initialized with undefined
console.log(x); // undefined
var x = 5;

// let/const are hoisted but not initialized (TDZ)
console.log(y); // ReferenceError
let y = 5;

Function Hoisting#

Code
// Function declarations are hoisted
sayHello(); // "Hello"

function sayHello() {
  console.log('Hello');
}

// Function expressions are NOT hoisted
sayHi(); // TypeError
const sayHi = () => {
  console.log('Hi');
};

Temporal Dead Zone#

The TDZ is the time between entering scope and variable initialization where the variable cannot be accessed.

Article discussion

(0)

Join the discussion — login to comment and help other developers.

Login to comment