1. Variable Promotion
Depending on how JavaScript operates and JavaScript does not have a block-level scope, it can be concluded that the variable declares that the elevation is moved to the top of the scope scope (global domain or current function scope).
- Variable declaration promoted to global domain
// undefined var a = "Hi", equivalent to var// variable promoted to global scope // declared variable A, but not initialized, Solid for undefineda= "Hi";
- Variable declaration promoted to current function field
varName = "Boy"; (function () { if(typeofname = = ' undefined ') { varname = ' Girl '; Console.log (' Hi, ' +name); } Else{Console.log (' Hi, ' +name); } })();//Console Output Hi,girlequivalentvarName = "Boy"; (function () { varName//variable promoted to current function scope if(typeofname = = ' undefined ') {Name= ' Girl '; Console.log (' Hi, ' +name); } Else{Console.log (' Hi, ' +name); } })(); 2, undefined and undefined (not defined) differences
undefinedThere are three ways to express the meaning:
- This variable exists , but does not give any value.
- A common data type and a value. You can manually assign any variable to undefined, at which point it has no special meaning.
- A value in an existing object that does not exist (not declared) is considered to be undefined.
To summarize these three kinds of cases, undefined can be summarized as follows:
- It is a data type and a value.
- When this value is present, either it is assigned to itself, or it exists, or the object in which it exists. A completely non-existent variable will not be undefined.
- Undefined (not defined)
A 未定义 variable (not defined) is a variable that does not have any declarations at all . Such a variable will throw a fatal error directly when used. However, if you use TypeOf to judge such a variable, it will not be error-prone and return undefined , which makes it impossible to use TypeOf to differentiate between the two cases.
JS Variable declaration Promotion