Skip to content

Instantly share code, notes, and snippets.

@manavm1990
Created April 25, 2025 01:14
Show Gist options
  • Save manavm1990/5b63de58d976777f3987c26eaa37d86b to your computer and use it in GitHub Desktop.
Save manavm1990/5b63de58d976777f3987c26eaa37d86b to your computer and use it in GitHub Desktop.

Understanding Hoisting in JavaScript

Hoisting is a JavaScript-specific behavior that moves variable and function declarations to the top of their scope during the compile phase. This means that you can use variables and functions before they’re declared in your code. While this might sound convenient, it can also lead to confusion and bugs if you're not careful.

How Hoisting Works

In JavaScript, there are two main types of hoisting:

  1. Variable Hoisting
    Variables declared with var are hoisted to the top of their scope, but only their declarations are hoisted. The initialization (assignment of a value) remains in place. This can lead to unexpected results.

    console.log(myVar); // Output: undefined
    var myVar = 10;

    In the example above, the var myVar declaration is hoisted, but the assignment (= 10) is not. This behavior is why the value of myVar is undefined before the assignment.

  2. Function Hoisting
    Function declarations are fully hoisted, meaning you can call the function before declaring it in your code.

    sayHello(); // Output: "Hello!"
    function sayHello() {
      console.log("Hello!");
    }

    However, function expressions (including those created with const or let) are not hoisted in the same way. This will result in an error:

    sayHello(); // ReferenceError: Cannot access 'sayHello' before initialization
    const sayHello = function () {
      console.log("Hello!");
    };

Why Hoisting is Less Relevant Today

In modern JavaScript development, hoisting is less of a concern because let and const have largely replaced var for variable declarations. Unlike var, variables declared with let and const are not initialized until their declaration is reached in the code. They exist in a "temporal dead zone" from the start of the block until the declaration is encountered, which prevents the use of uninitialized variables.

console.log(myLet); // ReferenceError: Cannot access 'myLet' before initialization
let myLet = 20;

console.log(myConst); // ReferenceError: Cannot access 'myConst' before initialization
const myConst = 30;

Hoisting Best Practices

In real-world development, hoisting is rarely a problem if you follow a consistent code structure. By organizing your code logically, you can avoid issues caused by hoisting entirely:

  1. Start with Imports
    Place all import statements at the very top of your files.

    import fs from 'fs';
    import path from 'path';
  2. Declare Variables and Constants Early
    Group all let and const declarations together at the beginning of their scope.

    const API_URL = 'https://api.example.com';
    let userData = null;
  3. Define Functions Before Usage
    Declare all named functions before you call them in your business logic.

    function fetchData() {
      // function implementation
    }
    
    fetchData();
  4. Keep Business Logic Last
    Write your program's primary logic at the bottom of the file, relying on the previously declared functions and variables.

Hoisting and Interviews

While hoisting is common as an interview topic, it's rarely an issue in modern JavaScript development. Interviewers may ask about hoisting to test your understanding of JavaScript internals and/or because they are just unoriginal, but in day-to-day coding, consistent code organization and the use of let and const effectively mitigate hoisting-related problems.

Key Takeaways

  • Hoisting moves variable and function declarations to the top of their scope.
  • Variables declared with var are partially hoisted, while let and const declarations are not initialized until their declaration.
  • Function declarations are fully hoisted, but function expressions are not.
  • Following a logical code structure with imports, declarations, and business logic can eliminate potential hoisting issues.

In summary, hoisting is one of those "gotcha" concepts that you should know about but rarely encounter in well-organized, modern JavaScript code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment