Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      June 4, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      June 4, 2025

      How To Prevent WordPress SQL Injection Attacks

      June 4, 2025

      Smashing Animations Part 4: Optimising SVGs

      June 4, 2025

      I test AI tools for a living. Here are 3 image generators I actually use and how

      June 4, 2025

      The world’s smallest 65W USB-C charger is my latest travel essential

      June 4, 2025

      This Spotlight alternative for Mac is my secret weapon for AI-powered search

      June 4, 2025

      Tech prophet Mary Meeker just dropped a massive report on AI trends – here’s your TL;DR

      June 4, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Beyond AEM: How Adobe Sensei Powers the Full Enterprise Experience

      June 4, 2025
      Recent

      Beyond AEM: How Adobe Sensei Powers the Full Enterprise Experience

      June 4, 2025

      Simplify Negative Relation Queries with Laravel’s whereDoesntHaveRelation Methods

      June 4, 2025

      Cast Model Properties to a Uri Instance in 12.17

      June 4, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      My Favorite Obsidian Plugins and Their Hidden Settings

      June 4, 2025
      Recent

      My Favorite Obsidian Plugins and Their Hidden Settings

      June 4, 2025

      Rilasciata /e/OS 3.0: Nuova Vita per Android Senza Google, Più Privacy e Controllo per l’Utente

      June 4, 2025

      Rilasciata Oracle Linux 9.6: Scopri le Novità e i Miglioramenti nella Sicurezza e nelle Prestazioni

      June 4, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Scoping, Hoisting and Temporal Dead Zone in JavaScript

    Scoping, Hoisting and Temporal Dead Zone in JavaScript

    April 17, 2025

    Before mastering JavaScript, it’s crucial to understand how it thinks behind the scenes. Concepts like scope, hoisting, and the temporal dead zone might sound tricky at first, but they form the backbone of how your code behaves.
    In this blog, we’ll break down these key ideas in the simplest way possible—so you can write cleaner code, avoid unexpected bugs, and truly understand what’s happening when your script runs.

    What is Scope in JavaScript?

    Think of scope like a boundary or container that controls where you can use a variable in your code.

    In JavaScript, a variable is only available in the part of the code where it was created. If you try to use it outside that area, it won’t work—that’s because of scope.

    This helps in two big ways:

    • Keeps your code safe – Only the right parts of the code can access the variable.
    • Avoids name clashes – You can use the same variable name in different places without them interfering with each other.

    JavaScript mainly uses two types of scope:

    1.Global Scope – Available everywhere in your code.

    2.Local Scope – Available only inside a specific function or block.

     

    Global Scope

    When you start writing JavaScript code, you’re already in the global scope—this is like the outermost area of your code where variables can live.

    If you create a variable outside of any function or block, it’s considered global, which means it can be used anywhere in your code.

    var a = 5; // Global variable
    function add() {
      return a + 10; // Using the global variable inside a function
    }
    console.log(window.a); // 5
    

    In this example, a is declared outside of any function, so it’s globally available—even inside add().

    A quick note:

    • If you declare a variable with var, it becomes a property of the window object in browsers.
    • But if you use let or const, the variable is still global, but not attached to window.
    let name = "xyz";
    function changeName() {
      name = "abc";  // Changing the value of the global variable
    }
    changeName();
    console.log(name); // abc
    

    In this example, we didn’t create a new variable—we just changed the value of the existing one.

    👉 Important:
    If you redeclare a global variable inside a function (using let, const, or var again), JavaScript treats it as a new variable in a new scope—not the same one. We’ll cover that in more detail later.

     

     Local Scope

    In JavaScript, local scope means a variable is only accessible in a certain part of the code—usually inside a function or a block.

    There are two types of local scope:

    1.Functional Scope

    Whenever you create a function, it creates its own private area for variables. If you declare a variable inside a function, it only exists inside that function.

    let firstName = "Shilpa"; // Global
    function changeName() {
      let lastName = "Syal"; // Local to this function
    console.log (`${firstName} ${lastName}`);
    }
    changeName();
    console.log (lastName); // ❌ Error! Not available outside the function
    

    You can even use the same variable name in different functions without any issue:

    function mathMarks() {
      let marks = 80;
      console.log (marks);
    }
    function englishMarks() {
      let marks = 85;
      console.log (marks);
    }
    

    Here, both marks variables are separate because they live in different function scopes.

     

    2.Block Scope

    Thanks to let and const, you can now create variables that only exist inside a block (like an if, for, or {}).

     

    function getMarks() {
      let marks = 60;
      if (marks > 50) {
        const points = 10;
        console.log (marks + points); // ✅ Works here
      }
      console.log (points); // ❌ Uncaught Reference Error: points is not defined
    }
    

     As points variable is declared in if block using the let keyword, it will not be only accessible outside as shown above. Now try the above example using var keyword i.e declare “points” variable with var and spot the difference.

    LEXICAL SCOPING & NESTED SCOPE:

    When you create a function (outer function) that contains another function (inner function), then the inner function has access to the outer function’s variables and methods. This is known as Lexical Scoping.

    function outerFunction() {
      let outerVar = "I’m outside";
      function innerFunction() {
          console.log (outerVar); // ✅ Can access outerVar
      }
      innerFunction();
    }
    

    In other terms, variables & methods defined in parent function are automatically available to its child functions. But it doesn’t work the other way around—the outer function can’t access the inner function’s variables.

    Hostinger

     

    VARIABLE SCOPE OR VARIABLE SHADOWING:

    You can declare variables with the same name at different scopes. If there’s a variable in the global scope and you create variable with the same name in a function, then you will not get any error. In this case, local variables take priority over global variables. This is known as Variable shadowing, as inner scope variables temporary shadows the outer scope variable with the same name.

    If the local variable and global variable have the same name then changing the value of one variable does not affect the value of another variable.

    let name = "xyz"
    function getName() {
      let name = "abc"            // Redeclaring the name variable
          console.log (name)  ;        //abc
    }
    getName();
    console.log (name) ;          //xyz
    

    To access a variable, JS Engine first going to look in the scope that is currently in execution, and if it doesn’t find there, it will look to its closest parent scope to see if a variable exist there and that lookup process will continue the way up, until JS Engine reaches the global scope. In that case, if the global scope doesn’t have the variable, then it will throw a reference error, as the variable doesn’t exist anywhere up the scope chain.

    let bonus = 500;
    function getSalary() {
     if(true) {
         return 10000 + bonus;  // Looks up and finds bonus in the outer scope
      }
    }
       console.log (getSalary()); // 10500
    

     

    Key Takeaways: Scoping Made Simple

    Global Scope: Variables declared outside any function are global and can be used anywhere in your code.

    Local Scope: Variables declared inside a function exist only inside that function and disappear once the function finishes.

    Global Variables Last Longer: They stay alive as long as your program is running.

    Local Variables Are Temporary: They’re created when the function runs and removed once it ends.

    Lexical Scope: Inner functions can access variables from outer functions, but not the other way around.

    Block Scope with let and const: You can create variables that exist only inside {} blocks like if, for, etc.

    Same Name, No Clash: Variables with the same name in different scopes won’t affect each other—they live in separate “worlds.” 

    Hoisting

    To understand Hoisting in JS, it’s essential to know how execution context works. Execution context is an environment where JavaScript code is executed.

    It has two main phases:

    1.Creation Phase: During this phase JS allocated memory or hoist variables, functions and objects. Basically, hoisting happens here.

    2.Execution Phase: During this phase, code is executed line by line.

    -When js code runs, JavaScript hoists all the variables and functions i.e. assigns a memory space for those variables with special value undefined.

     

    Key Takeaways from Hoisting and let’s explore some examples to illustrate how hoisting works in different scenarios:

    1. functions– Functions are fully hoisted. They can invoke before their declaration in code.
    foo (); // Output: "Hello, world!"
     function foo () {
         console.log ("Hello, world!");
     }
    
    1. var – Variables declared with var are hoisted in global scope but initialized with undefined. Accessible before the declaration with undefined.
    console.log (x); // Output: undefined
     var x = 5;
    

    This code seems straightforward, but it’s interpreted as:

    var x;
    console.log (x); // Output: undefined
     x = 5;
    

    3.Let, Const – Variables declared with Let and const are hoisted in local scope or script scope but stay in TDZ. These variables enter the Temporal Dead Zone (TDZ) until their declaration is encountered. Accessing in TDZ, results is reference Error

    console.log (x); // Throws Reference Error: Cannot access 'x' before initialization
     let x = 5;
    
    
    

    What is Temporal Dead Zone (TDZ)?

    In JavaScript, all variable declarations—whether made using var, let, or const—are hoisted, meaning the memory for them is set aside during the compilation phase, before the code actually runs. However, the behaviour of hoisting differs based on how the variable is declared.

    For variables declared with let and const, although they are hoisted, they are not initialized immediately like var variables. Instead, they remain in an uninitialized state and are placed in a separate memory space. During this phase, any attempt to access them will result in a Reference Error.

    This period—from the start of the block until the variable is initialized—is known as the Temporal Dead Zone (TDZ). It’s called a “dead zone” because the variable exists in memory but cannot be accessed until it has been explicitly declared and assigned a value in the code.

    console.log (x); //x is not defined -- Reference Error.
    let a=10; //b is undefined.
    var b= 100; // you cannot access a before initialization Reference Error.
    

    👉 Important: The Temporal Dead Zone helps prevent the use of variables before they are properly declared and initialized, making code more predictable and reducing bugs.

     

    🧾 Conclusion

    JavaScript hoisting and scoping are foundational concepts that can feel tricky at first, but once you understand them, they make your code more structured and predictable. Hoisting helps explain why some variables and functions work even before they’re declared, while scoping defines where your variables live and how accessible they are. By keeping these concepts in mind and practicing regularly, you’ll be able to write cleaner, more reliable JavaScript. The more you experiment with them, the more confident you’ll become as a developer. Keep learning, keep building, and everything will start to click. Happy coding! 🙌

     

     

    Source: Read More 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous ArticleExact Match Search with Sitecore Search
    Next Article The AI Mantra of All Time to Recite Now!

    Related Posts

    Security

    HPE StoreOnce Faces Critical CVE-2025-37093 Vulnerability — Urges Immediate Patch Upgrade

    June 4, 2025
    Security

    Google fixes Chrome zero-day with in-the-wild exploit (CVE-2025-5419)

    June 4, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Ready to Simplify Trust Management? Join Free Webinar to See DigiCert ONE in Action

    Development

    laravel-lang/attributes

    Development

    Final Fantasy IX fans think this new website hints at the long rumored remake

    News & Updates

    Laravel SaaS: 9 Useful Packages and Tools

    Development

    Highlights

    Apple launches iOS 18 Beta 3 – here’s everything you need to know

    July 10, 2024

    RCS text messaging is rolling out to more carriers, while the Photos app gets further…

    Hackers Exploit MS Equation Editor Vulnerability to Deploy XLoader Malware

    April 30, 2025

    CVE-2025-5527 – Tenda RX3 Stack-Based Buffer Overflow Vulnerability

    June 3, 2025

    CVE-2025-5580 – CodeAstro Real Estate Management System SQL Injection Vulnerability

    June 4, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.