JavaScript scope

The scope is at the base closure and manages the variables’ availability and defines the idea of a global and local variable. In other words, the scope is an encapsulation mechanism for code blocks, functions, and modules. ES6 and CommonJS modules also create a scope, unless the variable is explicitly exported. Scopes can be nested, and we can use the terms “outer” and “inner” scope for nesting. The inner scope can access the variable of its outer scope. The variables declared inside the global scope are called “global” variables and can be accessed from any scope. Browser supplies window
and document
global variables. Code block does not create scope for var
variables; only the function body does.
Static scoping (lexical scoping) — means that the “inner” scope can access the variable of its outer scope. The variables’ accessibility is determined statically by the position of the variables within the nested function scope and consists of outer scopes.
Originally published at https://dev.to on September 4, 2020.