Understanding JavaScript Execution Context, Lexical Scope, and Closures: A Beginner's Guide

Understanding JavaScript Execution Context, Lexical Scope, and Closures: A Beginner's Guide
Photo by Danielle-Claude Bélanger / Unsplash

JavaScript can seem a bit daunting at first, especially with concepts like execution context, lexical scope, and closures. But fear not! This guide will break down these topics in a friendly way, making them easy to understand.

What is Execution Context?

Think of execution context as a "workspace" for your JavaScript code. Every time you run a piece of JavaScript, it’s executed within an execution context. Here’s what happens:

  1. Creation: When a function is called, a new execution context is created. This context includes:
    • The value of this.
    • The local variables defined in the function.
    • The variables from outer scopes (more on this soon).
  2. Execution: After the context is created, JavaScript runs the code line by line within that context.

There are three main types of execution contexts:

  • Global Execution Context: The default context where your JavaScript code runs when it is not inside any function.
  • Function Execution Context: Created whenever a function is invoked.
  • Eval Execution Context: Created when code is executed inside an eval() function (not commonly used).

Example of Execution Context

let globalVar = "I'm a global variable";

function myFunction() {
    let localVar = "I'm a local variable";
    console.log(globalVar); // Accesses global variable
    console.log(localVar);   // Accesses local variable
}

myFunction();
// console.log(localVar); // This would cause an error because localVar is not defined in the global context

What is Lexical Scope?

Lexical scope is a way of defining how variables can be accessed within different contexts. It means that a function’s scope is determined by where it is declared, not where it is executed.

Key Points about Lexical Scope

  • A function can access variables from its own scope and the scope of its parent (the surrounding function).
  • Inner functions can access variables from outer functions, but not the other way around.

Example of Lexical Scope

function outerFunction() {
    let outerVar = "I'm from the outer function";

    function innerFunction() {
        console.log(outerVar); // Accesses outerVar from the outer function
    }

    innerFunction();
}

outerFunction();

What are Closures?

Closures are a powerful feature in JavaScript that comes from the combination of functions and lexical scope. A closure is created when a function retains access to its lexical scope, even when the function is executed outside that scope.

Why Are Closures Useful?

Closures allow you to:

  • Maintain state in an asynchronous environment.
  • Create private variables that cannot be accessed directly from outside the function.

Example of a Closure

function makeCounter() {
    let count = 0; // `count` is a private variable

    return function() {
        count += 1; // The inner function has access to `count`
        return count;
    };
}

const counter = makeCounter();

console.log(counter()); // Outputs: 1
console.log(counter()); // Outputs: 2
console.log(counter()); // Outputs: 3

In this example, count is a private variable that can only be modified by the inner function returned by makeCounter. Each time you call counter(), it remembers the value of count.

Remember This

Understanding execution context, lexical scope, and closures is crucial for mastering JavaScript. These concepts not only help you write cleaner and more efficient code but also empower you to utilize JavaScript’s powerful features.

Practice, practice and practice. It's true that practice makes perfect.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe