JavaScriptAdvanced
Deep Dive into JS Closures & Scopes
Duration: 15 minsAugust 14, 2025
Sponsored Advertisement SlotAdSense Responsive In-Article Banner
JS Closures & Lexical Scope
A closure is the combination of a function bundled together with references to its surrounding state (lexical environment).
1. Lexical Scope Example
function outer() {
const secret = 'encrypted-key';
function inner() {
console.log(secret); // Accesses parent scope variable!
}
return inner;
}
const getSecret = outer();
getSecret(); // Prints 'encrypted-key'