Scope

🏷️ Javascript

When executing JS code, the interpreter goes through code twice

All function params are implicitly declared in the function scope

Lexical scope

Compile-time scope: the decision for the scope is made after the first run

In the scond run, the interpreter assigns values to the variables and execute the functions The interpreter finds the declartion of the variables in the current scope before executing them. If they are not in the current scope, the interpreter goes up to the parent scope. The highest level scope is global scope. If the interpreter can’t find the variable’s declaration in the global scope, it throws ReferenceError If there are 2 same name variables, one in the current scope and one in the parent scope, the one in the current scope will hide the one in the parent scope

Function scope

A variable declared in the function has the function scope. The variable is available to the function itself and its children functions. The variable can’t be accessed from the outside

Block scope

let and const declare the variable in the block scope (if block, for block, a function)

IIFE (Immediately invoked function expression)

Allow a function to create a new block scope


https://www.telerik.com/blogs/understanding-scope-in-javascript