The scope is where values and expressions are available for use. Scopes can be treated as a pyramid where child scopes have access to parent scopes.
JavaScript has 3 types of scope:
- Global scope
- Function scope
- Block scope
Global Scope#
The scope that contains and is accessible in all other scopes
Function Scope#
The scope created with a function
Variables created inside a function scope cannot be accessed from outside the function
Block Scope#
The scope created with a pair of curly braces
Variables created inside a block scope cannot be accessed from outside the block
Blocks only scope let and const declarations, but not var declarations due to hoisting.