Gyaan

Lexical Scope

intermediate scope closures

A lexical scope in JavaScript means that a variable defined outside a function can be accessible inside another function defined after the variable declaration. But the opposite is not true; the variables defined inside a function will not be accessible outside that function.

In simple language, lexical scope is a variable defined outside your scope or upper scope is automatically available inside your scope which means you don’t need to pass it there.

Global Scope
var x = 2
add() Scope
var y = 1
✓ can access x from outer scope
✗ cannot access y here
var x = 2;
var add = function() {
    var y = 1;
    return x + y;
};