0
How does scope chaining help in variable resolution?
subina kallyani
easy
0completed
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: 60Here’s what happens:
cis found in the inner scope,bis found in the outer scope,ais 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