JavaScript scope and closures

JavaScript variables, lexical scope, and closures

Learn JavaScript variables, lexical scope, and closures with focused lessons on let, const, var, global object behavior, function scope, closure patterns, function objects, new Function, bind, and arrow function scope.

Variable, scope, and closure lessons

  1. Variables with let and const

    Declare named values, choose let or const, and understand how variable names make JavaScript programs readable.

    Outcome: Store and update values without relying on unclear global state.

    • JavaScript variables
    • let const JavaScript
    • declare variable
    Study Variables
  2. Lexical scope and closures

    Understand how functions keep access to outer variables from the environment where they were created.

    Outcome: Use closures for callbacks, factories, encapsulation, and asynchronous code.

    • JavaScript closures
    • lexical scope
    • variable scope
    Study Variable scope, closure
  3. The old var keyword

    Compare var with let and const so older code, hoisting, and function-scoped variables are easier to read.

    Outcome: Recognize legacy declarations and avoid var-specific surprises in modern code.

    • var JavaScript
    • let vs var
    • function scope
    Study The old "var"
  4. Global object

    Learn what belongs on the global object and why modern JavaScript avoids leaking values into global scope.

    Outcome: Keep application state explicit instead of depending on accidental globals.

    • JavaScript global object
    • global scope
    • window object
    Study Global object
  5. Function object and names

    Inspect function object metadata, named function expressions, and callable values stored in variables.

    Outcome: Understand functions as values that also carry object-like metadata.

    • function object
    • named function expression
    • function name
    Study Function object, NFE
  6. New Function and scope

    Study the dynamic Function constructor and how its scope differs from ordinary function declarations.

    Outcome: Recognize why generated functions should be rare and deliberate.

    • new Function
    • Function constructor
    • dynamic scope JavaScript
    Study The "new Function" syntax
  7. Function binding

    Use bind to preserve this and partially apply arguments when functions move between scopes and callers.

    Outcome: Keep method context predictable in callbacks and handlers.

    • JavaScript bind
    • this binding
    • partial application
    Study Function binding
  8. Arrow functions and lexical this

    Revisit arrow functions after learning scope, this, and closures so their lexical behavior is clear.

    Outcome: Choose arrow functions when lexical this is useful and avoid them when dynamic this is required.

    • arrow function this
    • lexical this
    • JavaScript arrow functions
    Study Arrow functions revisited