JS declaration variable using var declaration vs. Var declaration not used

Source: Internet
Author: User

JS in the variable declaration of explicit and implicit declarations.

The code is as follows Copy Code

VAR i=100;//Explicit declaration

i=100;//Implicit declaration of


Variables that are explicitly declared using the var keyword in a function are local variables instead of the VAR keyword, which is declared as a global variable using the direct assignment method.
 
When we use access to a variable that is not declared, JS will make an error. And when we assign a value to a variable that is not declared, JS does not give an error, instead it thinks that we are implicitly declaring a global variable, and this must be noted

The code is as follows Copy Code


var num = 1;

is to declare a variable in the current domain. If declared in a method, it is a local variable, or a global variable if declared in the global domain.


and

The code is as follows Copy Code
num = 1;

is actually an assignment to a property. First, it tries to do so in the current scope chain (as declared in the method, the current scope chain represents global scope and method local scope etc ...). , if NUM is found in any current scope chain, the Num property is assigned, and if NUM is not found, it creates and assigns the NUM attribute in the global object (that is, the topmost object in the current scope chain, such as the Window object).

Attention! Instead of declaring a global variable, it creates a property of a global object.

Even so, it may be difficult for you to understand the difference between the variable declaration and the Create object attribute. In fact, JavaScript variable declarations, creation attributes, and each property in each JavaScript have flags that indicate their properties----such as read-only (ReadOnly) Dontenum (Dontdelete), and so on.

Because a variable declaration takes a property that is not able to be deleted, the comparison of var num = 1 with num = 1, which is a variable declaration, with a property that cannot be deleted, and therefore cannot be deleted, is a property of the global variable and can therefore be removed from the global variable.


You can declare JavaScript variables by using the VAR statement:

The code is as follows Copy Code
var x;
var carname;
<!--[If!supportlinebreaknewline]-->
<!--[endif]-->

After the above declaration, the variables do not have a value, but you can assign values to the variables when you declare them:

The code is as follows Copy Code
var x=5;
var carname= "Volvo";

Note: When assigning a text value to a variable, enclose the value in quotation marks.

Assigning values to JavaScript variables
To assign a value to a JavaScript variable by an assignment statement:

The code is as follows Copy Code
x=5;
Carname= "Volvo";

The variable name is to the left of the = symbol, and the value you want to assign to the variable is to the right of =.

After the above statement is executed, the value saved in the variable x is 5, and the value of Carname is Volvo.
Assigning values to an undeclared JavaScript variable

If the variable you are assigning has not been declared, the variable is declared automatically.
These statements:

The code is as follows Copy Code
x=5;
Carname= "Volvo";

The same effect as these statements:

The code is as follows Copy Code
var x=5;
var carname= "Volvo";

Cases

The code is as follows Copy Code

NUM1 is a global variable and num2 is a property of window

var num1 = 1;

num2 = 2;

Delete num1; Cannot delete

Delete num2; Delete

function model () {

var num1 = 1; Local variables

num2 = 2; Window's Properties

anonymous functions

(function () {

var num = 1; Local variables

NUM1 = 2; Inheriting scopes (closures)

NUM3 = 3; Window's Properties

}())

}

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.