Understanding JavaScript Scope

Written by
Chris K.
7 mins
Aug 18, 2025
Subscribe for more insights
Thank you for your interest in the syllabus.

We'll be in touch soon with more information!

In the meantime, if you have any questions, don’t hesitate to reach out to our team.

Oops! Something went wrong while submitting the form.

TLDR - JavaScript Scope: Lexical, Block, and Hoisting Basics

  • Scope defines how and where variables are accessed in JavaScript.
  • JavaScript uses lexical scope, meaning scope is determined by a variable's location in the source code.
  • There are two types of lookups: LHS (assignment) and RHS (value retrieval).
  • Strict mode enforces cleaner code by throwing errors for undeclared variables.
  • Block scope (with let and const) restricts variable visibility to code blocks, unlike var, which is function-scoped.
  • Hoisting moves declarations to the top of their scope, but not initializations.
  • Nested scopes resolve variables from the inside out, allowing shadowing.
  • Scoping issues often appear in job interview questions, especially around loops and closures.
  • Understanding JavaScript scope is essential for writing even the simplest programs. While seemingly straightforward, this topic has numerous nuances that confuse many beginners. Most of these nuances fade into obscurity once developers switch from using var to let and const.

    Does that mean the topic can be skipped? Absolutely not. Thorough mastery of scope mechanics is frequently tested during job interviews.
    By understanding a few simple rules, you can significantly increase your chances of landing your dream job.

    What Is Scope?

    In programming languages, scope refers to the set of rules governing how variables are stored and looked up within a program.
    JavaScript uses lexical scope, meaning that the location where variables and functions are declared during compile time determines their scope.

    Think of scope as a set of nested containers - each block, function, or module represents a container that determines where variables can be accessed.

    Two Types of Lookups

    JavaScript engines perform two types of lookups when working with variables, depending on the context:

    LHS (Left-Hand Side)

    Happens when a variable is on the left side of an assignment operation. The engine searches for a memory location to store the value.


    Here, the engine finds or creates a place to store the value 42.

    RHS (Right-Hand Side)

    Happens when a variable's value is being accessed.

    The engine retrieves the value of foo to pass to console.log.

    Strict Mode vs. Non-Strict Mode

    JavaScript behaves differently depending on whether strict mode is enabled:

    • In strict mode, referencing a variable that doesn’t exist throws a ReferenceError.
    • In non-strict mode, the engine creates global variables if they are not declared (a potential source of bugs).

    Lexical Scope

    Lexical scope refers to how JavaScript determines the scope of a variable based on its location in the source code. Consider this example:

    Here, bar logs 2 because it looks at the scope where it was defined (global), not where it was called. If you’ve ever been puzzled by this behavior in JavaScript, you’re not alone - it’s a common "aha!" moment for many developers.

    Block Scope

    Block scope allows variables to exist only within a specific block (e.g., if, for). With let and const, JavaScript introduced true block scoping:

    In contrast, var is function-scoped, meaning its scope extends to the entire function:

    Hoisting

    Hoisting is JavaScript’s behavior of moving declarations to the top of their enclosing scope. However, only the declaration is hoisted - the initialization remains in place.

    Example:

    This behaves as if rewritten to:

    To avoid confusion, always declare variables at the top of their scope or use let/const:

    Nested Scopes

    Scopes can be nested. When a variable is accessed, the JavaScript engine starts in the current scope and moves outward:

    This example shows how each nested scope “shadows” variables from outer scopes. It’s one of those concepts that’ll feel intuitive once you’ve seen it in action a few times.

    Practical Example from Job Interviews

    A common interview question demonstrates issues with scoping in loops:

    The problem arises because var creates a single shared scope for the loop. By the time the returned function is called, the value of i has incremented to arr.length, and accessing arr[i] results in undefined. Here’s how to fix it:

    Using let

    Using IIFE:

    Conclusion

    Understanding scope is crucial for mastering JavaScript. Whether you’re preparing for interviews or building real-world projects, knowing how scope works will save you from countless bugs and help you write cleaner code. I can’t count how many times understanding these fundamentals has helped me debug a tricky issue.

    MDN Web Docs on Scope

    Find out how we cover AI/ML in our updated curriculum
    Get your Syllabus
    Special blog guest offer!

    Explore CS Prep further in our beginner-friendly program.

    Get 50% Off CS Prep
    Learning code on your own?

    Get more free resources and access to coding events every 2 weeks.

    Thank you for your interest in the syllabus.

    We'll be in touch soon with more information!

    In the meantime, if you have any questions, don’t hesitate to reach out to our team.

    Oops! Something went wrong while submitting the form.
    Want to learn more about advancing your career in tech?

    Connect with one of our graduates/recruiters.

    Schedule a Call

    Our graduates/recruiters work at:

    ABOUT THE AUTHOR

    Related Articles

    Introduction to Recursion in JavaScript

    JavaScript
    Tutorial
    by
    Everett Merrill and Alex Stewart
    Nov 21, 2025
    |
    12 minutes

    What Is Coding? A Plain-English Guide With Real Examples

    Skills
    Tutorial
    by
    Alex Stewart
    Nov 7, 2025
    |
    10 minutes

    JavaScript From Zero: Step by Step Guideline

    JavaScript
    Tutorial
    by
    Alex Stewart
    Oct 13, 2025
    |
    7 minutes

    Start your journey to a coding career.

    Thank you for your interest in the syllabus.

    We'll be in touch soon with more information!

    In the meantime, if you have any questions, don’t hesitate to reach out to our team.

    Oops! Something went wrong while submitting the form.
    Want to learn more about advancing your career in tech?

    Connect with one of our recruiters to learn about their journeys.

    Schedule a Call

    Our graduates/recruiters work at: