


Khusboo Tayal
Understanding scope and closures is crucial for mastering JavaScript. These two concepts are closely related and are essential for writing clean, efficient, and bug-free code.
Scope determines the accessibility (visibility) of variables in your code. It defines where a variable can be accessed and modified.
// Global Scope
let globalVariable = "I am global";
function showGlobal() {
console.log(globalVariable); // Accessible
}
showGlobal(); // Output: I am global
console.log(globalVariable); // Output: I am globalfunction showLocal() {
let localVariable = "I am local";
console.log(localVariable); // Accessible inside function
}
showLocal(); // Output: I am local
console.log(localVariable); // Error: localVariable is not definedif (true) {
let blockVariable = "I am block-scoped";
const anotherBlockVariable = "I am also block-scoped";
console.log(blockVariable); // Accessible here
}
console.log(blockVariable); // Error: blockVariable is not defined
console.log(anotherBlockVariable); // Error: anotherBlockVariable is not definedfunction outer() {
let outerVariable = "Outer Variable";
function inner() {
console.log(outerVariable); // Accessing outer variable
}
inner();
}
outer(); // Output: Outer VariableA closure is a function that remembers and can access variables from its lexical scope, even after the outer function has finished executing.
function outerFunction() {
let counter = 0;
function innerFunction() {
counter++;
console.log("Counter: " + counter);
}
return innerFunction;
}
const counterInstance = outerFunction();
counterInstance(); // Output: Counter: 1
counterInstance(); // Output: Counter: 2Closures are used in many practical scenarios, including:
Creating a function that generates other functions.
function createMultiplier(factor) {
return function (number) {
return number * factor;
};
}
const double = createMultiplier(2);
const triple = createMultiplier(3);
console.log(double(5)); // Output: 10
console.log(triple(5)); // Output: 15
Using closures to create private variables.
function createCounter() {
let count = 0;
return {
increment: function () {
count++;
console.log("Count: " + count);
},
reset: function () {
count = 0;
console.log("Counter reset");
}
};
}
const counter = createCounter();
counter.increment(); // Output: Count: 1
counter.increment(); // Output: Count: 2
counter.reset(); // Output: Counter resetClosures allow functions to access the outer function variables even after the outer function is executed.
<button id="myButton">Click Me</button>
<script>
function setupButton() {
let clickCount = 0;
document.getElementById("myButton").addEventListener("click", function() {
clickCount++;
console.log("Button clicked: " + clickCount + " times");
});
}
setupButton();
</script>
function createArray() {
let arr = [];
for (var i = 0; i < 5; i++) {
arr.push(function() {
console.log(i);
});
}
return arr;
}
const functions = createArray();
functions[0](); // Output: 5 (Not 0)
functions[1](); // Output: 5 (Not 1)
Use let instead of var (block scope).
function createArray() {
let arr = [];
for (let i = 0; i < 5; i++) {
arr.push(function() {
console.log(i);
});
}
return arr;
}
const functions = createArray();
functions[0](); // Output: 0
functions[1](); // Output: 1