arrow_left_alt Back to Blog

2Minutes: Variable Hoisting In JavaScript (let, and const keyword)

calendar_today July 14, 2026 | timer 2 min read |

What Is Variable Hoisting ?


First thing to know: const and let behave exactly the same way when gets hoisted. So that I will use let/const interchangebly in my examples !

Take a look at the following code:

console.log(name); // Output:  ReferenceError: Cannot access 'name' before initialization

const name = "Mouhammad";

console.log(name); // Output: Mouhammad

Both let and const are hoisted. However, they can’t be accessed before their declaration because they’re in the Temporal Dead Zone (TDZ). On the other hand, variables declared with var are hoisted and initialized with the default value undefined, which is why they can be accessed before their declaration.

Now it’s time for the real thing: function expressions. Their behavior during hoisting depends on which variable declaration keyword is used.

The following key points will make the explanation 10× easier:

  • The variable holding the function expression is hoisted.
  • let and const behave the same
  • Function expressions don’t have their own hoisting behavior. Instead, they inherit the hoisting behavior of the variable they’re assigned to.

let and const:

sayHello("Mouhammad"); // ReferenceError: Cannot access 'sayHello' before initialization

// sayHello is in the TDZ

const sayHello = (name) => console.log('Hello ' + name);

var:

sayHello("Mouhammad"); // TypeError: sayHello is not a function

// sayHello value now is undefined, but no such undefined() function

var sayHello = (name) => console.log('Hello ' + name);

That’s 2Minutes Concept, see you in the next one.

Sources


https://mouhammad.online/2minutes-function-hoisting-in-javascript/
https://mouhammad.online/2minutes-variable-hoisting-in-javascript-var-keyword/

index.php