+ 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.

21st Jul 2020, 2:44 PM
Samir Singh
Samir Singh - avatar
1 Answer
+ 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.
21st Jul 2020, 3:39 PM
Ore
Ore - avatar