Facebook Pixel

Nested Functions and Scope Resolution in JavaScript

Inner functions resolve variables through the scope chain. Here is how nesting and lexical scope work together.

Nested Functions and Scope Resolution in JavaScript

When a function is defined inside another function, it forms a nested scope. Variable resolution walks up this nesting via the scope chain.

Lexical (Static) Scope

JavaScript uses lexical scope. The scope chain is determined by where functions are written in the source, not where they are called.

const globalVar = "G"; function outer() { const outerVar = "O"; function inner() { const innerVar = "I"; console.log(innerVar, outerVar, globalVar); // I O G } inner(); } outer();

inner's scope chain is inner -> outer -> global, regardless of where inner is called from.

How Resolution Works

When the engine needs a variable:

  1. Look in the current variable environment.
  2. If not found, follow the outer environment reference to the parent.
  3. Repeat until found or the global scope is reached.
  4. If still not found, throw ReferenceError.

Closures Arise From This

If an inner function is returned or passed around, it keeps its outer environment reference. That reference is the closure.

function makeAdder(x) { return function (y) { return x + y; }; } const add5 = makeAdder(5); add5(3); // 8

The returned function still sees x even after makeAdder returned. The scope chain persists.

Shadowing

A local variable with the same name as an outer one shadows the outer:

const name = "Kunal"; function greet() { const name = "Asha"; console.log(name); // "Asha" } greet(); console.log(name); // "Kunal"

Inside greet, name resolves to the local one. The outer is hidden.

Dynamic Calls Do Not Change the Chain

function inner() { console.log(x); } function outer() { const x = 10; inner(); } const x = 99; outer(); // 99, not 10

inner was written in the global scope, so its chain is inner -> global. Calling it from outer does not change that. This is the key difference between lexical and dynamic scope.

The Takeaway

Nested functions resolve variables through a lexical scope chain built at write time. The chain does not change based on where the function is called. Returning an inner function keeps the chain alive (closure). Local names shadow outer ones.

Through the scope chain. The engine looks in the current variable environment, then follows the outer environment reference up to the lexical parent, repeating until the variable is found or the global scope is reached.

Lexically (statically) scoped. The scope chain is determined by where functions are written in the source, not where they are called. Dynamic calls do not change the chain.

When a local variable has the same name as a variable in an outer scope. Inside the inner scope, the local variable is used and the outer one is hidden. The outer is not affected.

No. Because JavaScript is lexically scoped, the scope chain is fixed at write time. Calling a function from a different location does not give it access to that location's variables.

A closure is an inner function that keeps its outer environment reference even after the outer function has returned. The scope chain persists, so the inner function can still access the outer variables.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.