Unlike programming languages such as C and Java, variables in JavaScript are of no type, and all variable definitions use keywords var:
The code copy is as follows:
var a;
var m, n;
var x=42, y="test";
If the variable is not assigned a value after defining it, the value of the variable is undefined. For example, the values of the three variables a, m and n in the above code are undefined.
Since variables in JS are of no type, it is possible to assign different types of values to the same variable, such as:
The code copy is as follows:
var b = "temp";
console.log(typeof b);//string
b = 108;
console.log(typeof b);//number
In addition to different types of assignments to the same variable, variables can also be repeatedly defined in JavaScript; if this is done, the variable definition statement after the first time is equivalent to the assignment statement:
The code copy is as follows:
var c = "hello";
console.log(c);//hello
var c = true;
console.log(c);//true
Under the strict mode of the ECMAScript standard, all variable definitions require the use of the var keyword. If strict mode is not used, when the JS program assigns a value to an undefined variable, the program will create a property in the JS global object with the same name as the variable, that is, create a new global variable. This approach will bring many problems (such as global variable pollution between multiple JS programs, etc.), which will cause considerable 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 during the variable definition process, then the variable will exist as a property of the global object. It can be obtained by accessing the corresponding properties of this (global object), or it can be deleted from the global object by using the delete keyword:
The code copy is as follows:
var e = "globalVariableValue";//defined outside of any function, it is a global variable, but does not store in "this"
f = "globalVariableValue2";
this.g = "globalVariableValue3";
console.log(this.e);//undefined
console.log(this.f);//globalVariableValue2
console.log(this.g);//globalVariableValue3
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 inside the function, JavaScript will define a nested local object inside the already defined local object. For a function, there are as many layers of nested function definitions as there are, as many layers of nested local objects as there are. This local object is called "function call object" ("call object" in ECMAScript 3, and was renamed "declarative environment record" in ECMAScript 5, but I personally think that the name in ECMAScript 3 is easier to understand).
In contrast to global objects 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 to understand some concepts in JavaScript, such as the scope and closure of variables.