⬅ Previous Topic
JavaScript Arrow FunctionsNext Topic ⮕
JavaScript Lexical Scope⬅ Previous Topic
JavaScript Arrow FunctionsNext Topic ⮕
JavaScript Lexical ScopeScope in JavaScript determines the visibility and accessibility of variables. Knowing where and how your variables are accessible is essential for writing clean, bug-free code.
JavaScript primarily deals with three types of scope:
Variables declared outside of any function or block are in the global scope. They can be accessed from anywhere in the code.
let name = "JavaScript"; // global variable
function greet() {
console.log("Hello, " + name);
}
greet(); // accessible
console.log(name); // also accessible
Hello, JavaScript
JavaScript
When you declare a variable inside a function using var
, let
, or const
, it's local to that function. You cannot access it outside the function.
function showAge() {
let age = 30;
console.log("Age is: " + age);
}
showAge();
console.log(age); // ReferenceError
Age is: 30
ReferenceError: age is not defined
Block scope is created using curly braces { }
. Only let
and const
support block-level scope. var
does not respect block boundaries.
{
let blockLet = "Scoped with let";
const blockConst = "Scoped with const";
var blockVar = "Function or Global Scoped";
console.log(blockLet); // accessible
console.log(blockConst); // accessible
console.log(blockVar); // accessible
}
console.log(blockVar); // accessible
console.log(blockLet); // ReferenceError
console.log(blockConst); // ReferenceError
Scoped with let
Scoped with const
Function or Global Scoped
Function or Global Scoped
ReferenceError: blockLet is not defined
ReferenceError: blockConst is not defined
var
is function-scoped, while let
and const
are block-scoped. This difference becomes crucial in loops and conditions.
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
3
3
3
Why? Because var
is function-scoped — the same variable i
is used in each iteration.
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
0
1
2
Why? Because let
is block-scoped — a new i
is created for each iteration.
Functions can access variables from their outer scope. This is called lexical scoping.
let language = "JavaScript";
function outer() {
let framework = "React";
function inner() {
console.log("Language: " + language);
console.log("Framework: " + framework);
}
inner();
}
outer();
Language: JavaScript
Framework: React
Shadowing occurs when a variable in a local scope has the same name as one in an outer scope. The local variable shadows the outer one.
let value = 100;
function test() {
let value = 200; // shadows outer 'value'
console.log(value);
}
test();
console.log(value);
200
100
Declaring too many global variables can cause conflicts — especially when integrating multiple scripts. To avoid this:
let
and const
instead of var
An IIFE is a function that runs immediately, creating its own local scope:
(function () {
let secret = "hidden";
console.log("Inside IIFE");
})();
console.log(secret); // Error
Inside IIFE
ReferenceError: secret is not defined
Mastering scope in JavaScript isn’t just about avoiding errors — it’s about designing better logic, writing safer code, and being in full control of variable access. Whether you’re debugging a tricky bug or architecting a new feature, understanding how scope works will always give you the upper hand.
Explore scope. Control visibility. Write smarter code.
⬅ Previous Topic
JavaScript Arrow FunctionsNext Topic ⮕
JavaScript Lexical ScopeYou 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.