I am online
← Back to Articles

JavaScript Hoisting: Memory Allocation and Execution

JavaScriptApril 14, 2024

Introduction

Hoisting in JavaScript does not move variables to the top. It occurs during the memory allocation phase, which happens before code execution. In this phase, var variables are set to undefined, and function references are created.

For example:

function yacine() {
  console.log(key);
  var key = "Hello frontYnova";
}
yacine();

The output is undefined because during the memory allocation phase, key is set to undefined. When console.log(key) is executed, it finds undefined in memory and logs it. Note that const and let have different behaviors in this phase.