Unlike programming languages such as C and Java, variables in JavaScript are of no type, and all variables are defined with the following keywords var:
Copy Code code as follows:
var A;
var m, N;
var x=42, y= "test";
If the variable is not assigned after the variable is defined, the value of the variable is undefined. The values for a, M, n three variables in the above code are undefined.
Because the variables in JS are of no type, you can definitely assign different types of values to the same variable, such as:
Copy Code code as follows:
var b = "Temp";
Console.log (typeof b);//string
b = 108;
Console.log (typeof b);//number
In addition to being able to assign different types of values to the same variable, JavaScript can also define a variable repeatedly, and if so, the first-time variable definition statement is equivalent to an assignment statement:
Copy Code code as follows:
var c = "Hello";
Console.log (c);//hello
var C = true;
Console.log (c);//true
Under the ECMAScript standard Strict mode (strict mode), all variable definitions need to use the var keyword. If you do not use strict mode, then when the JS program to an undefined variable to assign a value, the program will be in the JS global object to create a name and the same variable property, that is, create a new global variable. This practice will bring a lot of problems (for example, a number of JS program to produce global variable pollution, etc.), to the maintenance of the later bring no small trouble, so in the actual development process, should try to avoid the use of this practice.
Storage of variables
If the defined variable is a global variable and the VAR keyword is not used in the variable definition, the variable exists as a property of the global object and can be obtained by accessing the corresponding property of this (global object), or by removing it from the global object by using the Delete keyword:
Copy Code code as follows:
var e = "Globalvariablevalue";//defined outside of any function, it's 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 every function call in JavaScript, JavaScript creates a local object to store the local variables defined in the function, and if there is a nested defined function within the function (nested function), JavaScript then defines a nested local object within the defined local object. For a function, how many layers of nested function definitions are inside, and how many layers of nested local objects there are. This local object is referred to as the "Call object" in ECMAScript 3, and ECMAScript 5 is renamed "Declarative Environment Record", but the individual thinks it is ECMAScript The names in 3 are easier to understand.
In contrast to global object this, JavaScript does not provide any way to access these local objects (function call objects). Therefore, developers cannot manipulate these local objects. However, understanding these function invocation objects can be a great help in understanding some of the concepts in JavaScript, such as the scope of variables and closures.