⬅ Previous Topic
JavaScript Scope - Local, Global, BlockNext Topic ⮕
JavaScript Hoisting - var, let, function⬅ Previous Topic
JavaScript Scope - Local, Global, BlockNext Topic ⮕
JavaScript Hoisting - var, let, functionIn JavaScript, lexical scope (also known as static scope) refers to the fact that the scope of a variable is determined by its position in the source code. When a function is defined, it captures variables from its surrounding (parent) scope at that specific place in the code — this forms a lexical environment.
Understanding lexical scope is important for mastering how variables are accessed, especially in nested functions and closures. It helps avoid bugs and makes your code more predictable.
let outerVar = "I am outside!";
function outerFunction() {
console.log(outerVar); // Can access outerVar due to lexical scope
}
outerFunction();
I am outside!
Here, the function outerFunction()
can access outerVar
because it's defined in the outer (lexical) environment.
What if we define another variable inside the function? Can it be accessed outside the function?
No. Inner variables are not accessible from the outer scope. Lexical scope only works from outer to inner — not vice versa.
function outer() {
let outerMsg = "Hello from outer!";
function inner() {
console.log(outerMsg); // Can access because of lexical scope
}
inner();
}
outer();
Hello from outer!
Here, inner()
can access outerMsg
because it's defined inside outer()
, forming a lexical scope chain.
function wrapper() {
let secret = "Top Secret";
function reveal() {
return secret;
}
return reveal;
}
let func = wrapper();
console.log(func()); // Trying to access 'secret' indirectly
Top Secret
This example demonstrates closure — a function retaining access to its lexical scope even after the outer function has returned. Although secret
is not in the global scope, reveal
still has access to it.
Think of lexical scope like a series of boxes. If you're in an inner box, you can see and use anything in the boxes around you. But if you're outside, you can't peek inside the smaller boxes.
let a = "global";
function testScope() {
let a = "local";
console.log(a); // Refers to local 'a', not global
}
testScope();
console.log(a); // Refers to global 'a'
local global
This example proves that variables inside a function do not interfere with variables outside — thanks to lexical scoping.
let message = "Hello";
function greet() {
console.log(message);
}
function run() {
let message = "Hi";
greet(); // What will this print?
}
run();
Hello
Because greet()
was defined in the global scope, it retains access to the global message
, not the one in run()
.
⬅ Previous Topic
JavaScript Scope - Local, Global, BlockNext Topic ⮕
JavaScript Hoisting - var, let, functionYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.