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

      In-House vs. Outsource Node.js Development Teams: 9 Key Differences for the C-Suite (2025)

      July 19, 2025

      Why Non-Native Content Designers Improve Global UX

      July 18, 2025

      DevOps won’t scale without platform engineering and here’s why your teams are still stuck

      July 18, 2025

      This week in AI dev tools: Slack’s enterprise search, Claude Code’s analytics dashboard, and more (July 18, 2025)

      July 18, 2025

      DistroWatch Weekly, Issue 1131

      July 20, 2025

      I ditched my Bluetooth speakers for this slick turntable – and it’s more practical than I thought

      July 19, 2025

      This split keyboard offers deep customization – if you’re willing to go all in

      July 19, 2025

      I spoke with an AI version of myself, thanks to Hume’s free tool – how to try it

      July 19, 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

      The details of TC39’s last meeting

      July 20, 2025
      Recent

      The details of TC39’s last meeting

      July 20, 2025

      Simple wrapper for Chrome’s built-in local LLM (Gemini Nano)

      July 19, 2025

      Online Examination System using PHP and MySQL

      July 18, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 tests “shared audio” to play music via multiple devices, new taskbar animations

      July 20, 2025
      Recent

      Windows 11 tests “shared audio” to play music via multiple devices, new taskbar animations

      July 20, 2025

      WhatsApp for Windows 11 is switching back to Chromium web wrapper from UWP/native

      July 20, 2025

      DistroWatch Weekly, Issue 1131

      July 20, 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.

     

    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 

    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

    Artificial Intelligence

    Scaling Up Reinforcement Learning for Traffic Smoothing: A 100-AV Highway Deployment

    July 20, 2025
    Repurposing Protein Folding Models for Generation with Latent Diffusion
    Artificial Intelligence

    Repurposing Protein Folding Models for Generation with Latent Diffusion

    July 20, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Artificial Intelligence – What’s all the fuss?

    Development

    AOL blocks malicious ads from its advertising platform

    Development

    This AI Paper from ByteDance Introduces a Hybrid Reward System Combining Reasoning Task Verifiers (RTV) and a Generative Reward Model (GenRM) to Mitigate Reward Hacking

    Machine Learning

    Planet Technology Industrial Switch Flaws Risk Full Takeover – Patch Now

    Security

    Highlights

    CVE-2025-4178 – Xiaowei1118 Java Server Path Traversal Vulnerability

    May 1, 2025

    CVE ID : CVE-2025-4178

    Published : May 1, 2025, 10:15 p.m. | 1 hour, 12 minutes ago

    Description : A vulnerability was found in xiaowei1118 java_server up to 11a5bac8f4ba1c17e4bc1b27cad6d24868500e3a on Windows and classified as critical. This issue affects some unknown processing of the file /src/main/java/com/changyu/foryou/controller/FoodController.java of the component File Upload API. The manipulation leads to path traversal. The attack may be initiated remotely. The exploit has been disclosed to the public and may be used. This product takes the approach of rolling releases to provide continious delivery. Therefore, version details for affected and updated releases are not available.

    Severity: 5.4 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-46421 – Apache Libsoup HTTP Authorization Header Exposure Vulnerability

    April 24, 2025

    CVE-2025-30421 – NI Circuit Design Suite Stack-Based Buffer Overflow Vulnerability

    May 15, 2025
    11 Things to Do After Installing Ubuntu 25.04

    11 Things to Do After Installing Ubuntu 25.04

    April 18, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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