Interview Question

What is lexical scope?

Lexical scope follows code nesting, not the caller location.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

Lexical scope means identifier visibility is determined by source-code nesting. β€’ A function’s outer environment is chosen where the function is created. β€’ Calling the function elsewhere does not change that lexical parent. β€’ Closures depend on lexical scope.

Example

Code
const name = 'outer';
function show() { console.log(name); }
show();
Output
outer

Quick Revision

Lexical scope follows code nesting, not the caller location.