0

How does scope chaining help in variable resolution?

author
subina kallyani
easy
0
17

Answer

Scope chaining is the process JavaScript uses to find variables during code execution.
When you use a variable, JavaScript first looks for it in the current (local) scope.
If it’s not found, it moves outward — checking each outer (parent) scope — until it reaches the global scope.

This sequence of scopes forms the scope chain.


let a = 10;

function outer() {
let b = 20;

function inner() {
let c = 30;
console.log(a + b + c); // JavaScript finds a → b → c using scope chain
}

inner();
}

outer(); // Output: 60


Here’s what happens:

  • c is found in the inner scope,
  • b is found in the outer scope,
  • a is found in the global scope.

In short: Scope chaining helps JavaScript resolve variables correctly by searching from local to global scope, ensuring each variable is accessed from the right place.

Click to Reveal Answer

Tap anywhere to see the solution

Revealed

Comments0
    How does scope chaining help in variable resolution? - execution context, scope | EBAT