Scope of variables in Java Script
In JavaScript, scope refers to the visibility and accessibility of variables. It determines which parts of your code can “see” or use a specific variable based on where it was declared.
There are three primary types of scope in modern JavaScript:
1. Global Scope
A variable declared outside of any function or block is in the global scope. It is accessible from anywhere in your entire script, including inside functions and loops.
2. Function Scope
Variables declared inside a function are in function scope. They can only be accessed from within that specific function and are invisible to the rest of the program.
3. Block Scope
Introduced with ES6, block scope restricts variables to the specific set of curly braces
{} where they are defined, such as inside an if statement or a for loop.