+ 2
Closures v/s lexical scoping
Is closures and lexical scoping are same. If not then please explain what is the difference between closures and lexical scoping in Javascript and where both of these are used.
1 Odpowiedź
+ 1
Lexical scoping means that a variable(var, const or let) declared within a function can be accessed within any inner function declared after the variable was declared.
For example,
function outer() {
var x = 10;
function say10() {
console.log(x);
}
}
This is very useful in closures and IIFEs.
function say10() {
const x = 10;
(function() {
console.log(x);
})();
}
Hope you understand :)
Edit: The concept of lexical scoping differs among languages depending on the language design.