Scopes in javascript refers to the current context of code, which determines the accessibilty of variables to javascript. Javascript has the following kinds of scopes:-
- Global Scope
- Module Scope
- Function Scope
- Block Scope
- Lexical Scope
Variables defined outside any function, block, or module scope have everywhere in the application. when a module system is enabled it's harder to make global variables,but one can still do it. By defining a variable in HTML, outside any function, a global variable can be created.
Before modules, a variable declared outside any function was a global variable. In modules, a variable declared outside any function is hidden and not available to other modules unless it is explicity exported..
Block scope is defined with curly braces. it is separated by { and }. Variables declared with let and const can have block scope. They can only be accessed in the block in which they are defined.
Function scope means that parameters and variables defined in a function are visible everywhere within the function, but are not visible outside of the function.
Lexical scope is tha ability of the inner function to access the outer scope in which is defined.