Hoisting in JavaScript

Hoisting in JavaScript - Mariya Baig

What is Hoisting?

In JavaScript, hoisting is a behaviour where variable and function declarations are lifted to the top of their respective scopes during the compilation phase. This ensures that, no matter where you declare variables and functions in your code, they are effectively moved to the top of their scopes before the actual code execution begins. The term “hoisting” can be likened to picking up and placing something higher.

What is Execution Context?

Execution Context is the environment where your JavaScript code is running.

  • Memory Component:
    • Description: It is the backstage or storage area where the JavaScript engine prepares and keeps track of variables and functions.
    • Functionality: It scans through your code, finds any declarations of variables and functions, and creates memory blocks to store them. These blocks act as pairs with keys (variable/function names) and values (initially set to “undefined”).
    • Hoisting Connection: Declarations are hoisted to the top of their respective scopes during this phase, allowing them to be used anywhere in the code.
  • Code Component:
    • Description: It is the main stage where the actual performance of your code takes place.
    • Functionality: It executes your code line by line, from top to bottom. This includes performing actions like assigning values to variables, calling functions, using conditionals, and more.
    • Order of Execution: The code runs sequentially, following the order of your written statements.

In simple terms, the Memory Component is where JavaScript sets up and organizes the backstage details (like variables and functions), while the Code Component is where the real action happens on the main stage (executing your code). Understanding these components helps in grasping concepts like hoisting, where declarations are prepared in the Memory Component before the actual code runs in the Code Component.

Variable Hoisting

  • Declaration Moves to the Top:
    • When JavaScript is processing your code, it takes all the variable declarations and moves them to the top of their respective scopes.
  • Accessible Anywhere in the Scope:
    • This hoisting behaviour allows you to use a variable even before it’s formally declared in your code.

var hoisting

variables declared with var are hoisted and initialised with undefined during the hoisting phase

var hoisting error

The variable varHoisting is hoisted to the top during the compilation phase, but its value is assigned later in the code.

var hoisting

let hoisting

Variables declared with let are hoisted but not initialized during the hoisting phase.

let hoisting

Unlike var, the variable letHoisting is hoisted to the top but not initialized. Trying to access it before the declaration results in a ReferenceError.

let hoisting

const hoisting

Variables declared with const are hoisted but not initialized during the hoisting phase.

const hoisting

Similar to let, the variable constHoisting is hoisted but not initialized, causing a ReferenceError if accessed before the declaration.

const hoisting

Function Hoisting

Function hoisting is a behaviour in JavaScript where function declarations are moved to the top of their containing scope during the compilation phase. This means that, regardless of where you declare a function in your code, its declaration is effectively “lifted” to the top of its scope, allowing the function to be used before its actual placement in the code.

Function Declaration Hoisting

function hoisting

even though funcHoisting() is called before its actual declaration in the code, it works perfectly. The function declaration is hoisted to the top during the compilation phase, making the function available for use anywhere in its containing scope.

Function Expression Hoisting

Arrow functions, unlike function declarations, are not hoisted in the same way. Function declarations are hoisted to the top of their containing scope during the compilation phase, allowing them to be used before their actual placement in the code. Arrow functions, on the other hand, exhibit different behaviour.

Arrow functions are expressions, and their hoisting depends on how they are assigned. Let’s explore two scenarios

Arrow Function Assigned to a Variable

the arrow function is assigned to the variable myFunction. When you try to invoke myFunction before its declaration, a TypeError occurs because the variable is hoisted, but its value (the arrow function) is not initialized until the assignment line.

Arrow Function Assigned to a Constant

trying to invoke it before its declaration will result in a ReferenceError. This is because the variable is hoisted but not initialized until the assignment line.

arrow function hoisting with var
Arrow function declared with variable.
arrow function hoisting with const
Arrow function declared with const.

In summary, function hoisting allows you to use function declarations before their actual placement in the code, contributing to the flexibility of JavaScript code organization. Function expressions, on the other hand, exhibit different hoisting behaviour and should be declared before use to avoid errors.

Tips for Navigating Hoisting

Understanding hoisting is one thing, but using it wisely is another. Here are some practical tips:

  • Know the Initialization Order: Be aware of the order in which variables and functions are declared. Hoisting doesn’t move the assignments, just the declarations.
  • Use let and const: In ES6, the introduction of let and const provides block-scoping, reducing the likelihood of unintended consequences related to hoisting.
  • Don’t Rely Solely on Hoisting: While hoisting is part of JavaScript, it’s good practice to declare variables and functions at the beginning of their scopes. This improves code readability and reduces confusion.

Wrapping It Up

Hoisting might sound intimidating, but it’s a valuable part of JavaScript once you grasp its ins and outs. By understanding how hoisting works, developers can write cleaner, more predictable code, making their JavaScript adventures a bit smoother. So, don’t let hoisting be a mysteryโ€”embrace it and make it work for you!