Do you understand the difference between adding var in javascript and without var?

Source: Internet
Author: User

Do you understand the difference between adding var in javascript and without var?

Javascript is a product that follows the ECMAScript standard. Naturally, the ECMAScript standard must follow.

Let's take a look at the definition and usage of the var keyword.

The var statement is used to declare variables.

The creation of JavaScript variables is also called a "Declaration" variable:

Copy codeThe Code is as follows:
Var carName;

After the variable is declared, the variable is null (no value ).

For variable replication, the operation is as follows:

Copy codeThe Code is as follows:
CarName = "Volvo ";

When declaring a variable, you can also assign values to the variable:

Copy codeThe Code is as follows:
Var carName = "Volvo ";

Syntax

Copy codeThe Code is as follows:
Var varname = value;

Parameter Value

 

Parameters Description
Varname Required. Variable name.

Variable names can contain letters, numbers, underscores, and dollar signs.

  • The variable name must start with a letter
  • Variable names can also start with $ and _ (but are generally not used in this case)
  • Variable names are case sensitive (y and Y are different variables)
  • Reserved Words (such as JavaScript keywords) cannot be used as variable names
Value Optional. Specifies the value of a variable.

Note:If the variable declaration does not specify a value, its default value isUndefined

Everyone has read many articles, saying that you should avoid implicit declaration of global variables. That is to say, you must add 'var' before declaring a variable ', what is the difference between adding 'var' and not 'var?

Let's take a look at a piece of code.

Var a = 'a'; alert (a); // the 'A' alert (window. a) is displayed. // the 'A' is displayed'

The declaration of a global variable adds an attribute to the 'window' object. The following code has the same effect.

A = 'a'; alert (a); // the 'A' alert (window. a) is displayed. // the 'A' is displayed'

"Var a = 'A'" and "a = 'A'" are both global variables. What is the difference? See the following two sections of code:

var a = 'aa';delete window.a; // false a = 'aa';delete window.a; // true 

All added properties for the 'window' object. One can be deleted, and the other cannot be deleted. However, with 'var' added, the scope can be related. Without 'var', all attributes are dynamically added to the 'window' object. The following code proves that

Var test = function () {a = 'a';} test (); alert (window. a); // the 'A' is displayed'

Because the window object is a global object, you can leave it empty by default. The following section will show the same effect.

Var test = function () {a = 'aa';} test (); alert (a); // The 'aa' is displayed'

When talking about this, those who think about it carefully must have a question: why can I delete the implicitly declared global variables, but cannot delete the explicitly declared global variables?

The reason is that "delete cannot delete the properties with the configurability of false", and the properties of some built-in objects are not configurable, for example, to declare the attributes of a global object created through a variable declaration or function declaration, the following code proves

Delete Object. prototype; // false cannot be deleted. This attribute is not configurable var a = 'a'; delete window. a; // false cannot be deleted. This attribute is a function test () {}; delete window that cannot be configured. test; // false: the attribute cannot be deleted and cannot be configured.

In this case, we don't understand. The global variable declared through 'var' actually adds an unconfigurable attribute to the 'window' object, without the 'var' declared global variable, it actually adds a configurable attribute for the 'window' object.

Note that all the above windows can be replaced by this, for example:

Var test = function () {a = 'a';} test (); alert (this. a); // the 'A' is displayed'

For the reason, refer to the article 'this, this, and discuss this in javascript again'

Below we will pull out the var keyword in javascript and explain it separately.

We know that when defining a variable, we need to use the Var keyword. When using the Var keyword, we need to pay attention to its usage:
The following columns fully demonstrate the different execution results of Var when using and not using global variables and defining local variables.

var var01 = 1;function funtest() { document.write(var01); var var01 = 0;} 

The result is undefined.

var var01 = 1;function funtest() { document.write(var01); var01 = 0;} 

Result: 1

 var01 = 1;function funtest() { document.write(var01); var var01 = 0;}

The result is undefined.

var01 = 1;function funtest() { document.write(var01); var01 = 0;}

Result: 1

We can see how much you know about var in javascript. I believe that you will learn more or less through this article. For more information

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.