Because there are only two scopes in JS, there are only two types of variables.
1: global variables -> are globally accessible.
2: Local variables -> can only be accessed in the current scope.
There is a special case in JS where a variable declared in the scope of a function without the var word is a global variable.
Here’s another example:
Conclusion:
1: A subset scope is a variable that can access the parent scope (or upper level), but the parent scope is a variable that cannot access the subset scope.
2: When accessing a variable, first look in the current scope to see if the variable exists, the nearest principle, if not, look up one level (parent), if not, then look up until the global scope, if not, return a undefined.
3: Variables that are not declared using var are global variables and are not recommended.
4: Variables are destroyed when they are out of scope. Global variables are only destroyed when the page is closed or the browser is closed, so use global variables as little as possible. They take up memory space.