Skip to main content

Hoisting

Hoisting is a Javascript default behavior of moving functions, variables or classes declarations on top of the module/function level scope before executing the code.

Function declarations are hoists, while function expressions are not

// This is function declaration
function foo() { ... }

// This is function expression
var bar = function () { ... }

Variables hoisting

  • Variables declared with var hoisted to the top of its functional or global scope, but the initialization is not. If you try to use a variable before it is declared and initialized, it will have the value undefined.
  • Variables declared with let and const hoisted, but they are not initialized with a default value. Accessing them before the actual declaration results in a ReferenceError. This is often referred to as the "temporal dead zone."
console.log(a); // undefined, due to hoisting
var a = 5;

console.log(b); // ReferenceError: b is not defined
let b = 3;