Introduction to variable definition and storage in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the definition and storage of variables in JavaScript. This article first explains some techniques for variable definition, and then explains the storage of variables, if you need a variable, refer to the following different programming languages, such as C and Java. variables in JavaScript are non-typed, and all variables are defined using the keyword var:


The code is as follows:


Var;
Var m, n;
Var x = 42, y = "test ";


If the variable is not assigned a value after it is defined, the value of the variable is undefined. For example, the values of the variables a, m, and n in the above code are undefined.

Because the variables in JS are non-typed, you can assign values of different types to the same variable, for example:


The code is as follows:


Var B = "temp ";
Console. log (typeof B); // string
B = 108;
Console. log (typeof B); // number


In addition to assigning values of different types to the same variable, you can also define the variable repeatedly in JavaScript. if so, the variable definition statement after the first time is equivalent to the value assignment statement:


The code is as follows:


Var c = "hello ";
Console. log (c); // hello
Var c = true;
Console. log (c); // true


In the strict mode standard of ECMAScript, all variable definitions must use the var keyword. If the strict mode is not used, when the JS program assigns a value to an undefined variable, the program will create an attribute with the same name as the variable in the JS Global object, create a global variable. This approach will cause many problems (for example, global variable pollution among multiple JS programs), which will cause a lot of trouble for later maintenance. Therefore, in the actual development process, this approach should be avoided whenever possible.

Storage of variables

If the defined variable is a global variable and the var keyword is not used during the definition of the variable, the variable will exist as the attribute of the global object. you can access this (global object) you can also use the delete keyword to delete it from the global object:


The code 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 every callback function call in JavaScript, JavaScript creates a local object to store the local variables defined in the function. if there is a nested function inside the function ), javaScript then defines a nested local object within the defined local object. For a function, the number of layers of nested function definitions is also the number of layers of nested local objects. This local object is called a "function call object" ("call object" in ECMAScript 3, and ECMAScript 5 is renamed as "declarative environment record ", but I personally think it is easier to understand the name in ECMAScript 3 ).

Unlike the global object this, JavaScript does not provide any way to access these local objects (function call objects ). Therefore, developers cannot perform operations on these local objects. However, understanding these function call objects can be helpful for understanding some concepts in JavaScript, such as the scope and closure of variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.