Unlike programming languages such as C and Java, variables in JavaScript are untyped, and all variable definitions use the keyword var:
If a variable is not assigned a value after it is defined, the value of the variable is undefined. For example, the values of the three variables a, m, and n in the above code are all undefined.
Since variables in JS are typeless, it is completely possible to assign different types of values to the same variable, such as:
In addition to different types of assignments to the same variable, JavaScript can also define variables repeatedly; if you do this, the variable definition statement after the first time is equivalent to the assignment statement:
In the strict mode of the ECMAScript standard, all variable definitions must use the var keyword. If strict mode is not used, when a JS program assigns a value to an undefined variable, the program will create a property with the same name as the variable in the JS global object, that is, a new global variable will be created. This approach will cause many problems (for example, global variable pollution between multiple JS programs, etc.) and will bring a lot of trouble to later maintenance; therefore, in the actual development process, this approach should be avoided as much as possible.
Storage of variables
If the defined variable is a global variable and the var keyword is not used in the variable definition process, then the variable will exist as an attribute of the global object, which can be obtained by accessing the corresponding attribute of this (global object), or It can be deleted from the global object by using the delete keyword:
delete f;
delete g;
console.log(this.f);//undefined
console.log(this.g);//undefined
For each function call in JavaScript, JavaScript will create a local object to store the local variables defined in the function; if there is a nested function defined inside the function, JavaScript will create a local object after the function has been defined. Define a nested local object inside the local object. For a function, there are as many layers of nested local objects as there are layers of nested function definitions inside it. This local object is called a "function call object" ("call object" in ECMAScript 3, renamed "declarative environment record" in ECMAScript 5, but personally I think the name in ECMAScript 3 is easier to understand).
Contrary to the global object this, JavaScript does not provide any way to access these local objects (function call objects). Therefore, developers cannot operate on these local objects. However, understanding these function call objects will be of great help in understanding some concepts in JavaScript, such as variable scope and closures.