SoFunction
Updated on 2025-04-07

Do you really understand the difference between adding var and not adding var in javascript

Javascript is a product that follows the ECMAScript standard, and the natural ECMAScript standards must be followed.

Let’s first 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 "declaration" a variable:

Copy the codeThe code is as follows:

var carName;

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

For variable copying, the operation is as follows:

Copy the codeThe code is as follows:

carName = "Volvo";

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

Copy the codeThe code is as follows:

var carName = "Volvo";

grammar

Copy the codeThe code is as follows:

var varname = value;

Parameter value

 

parameter describe
varname must. Specify the 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 not in this way)
  • 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 the variable.

Notice: If the variable declaration does not specify a value, its default value is undefined

Everyone has read a lot of articles and says that they avoid implicitly declaring global variables, which means that 'var' must be added before declaring variables. So what is the difference between adding 'var' and not adding 'var'?

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

var a = 'aa';
alert(a); //Popt 'aa'alert()//pop up'aa' 

You understand, you declare a global variable to actually add a property to the 'window' object. The following code has the same effect

a = 'aa';
alert(a); //Popt 'aa'alert()//pop up'aa' 

Then "var a = 'aa' " and "a = 'aa' " are both global variables. What's the difference? Look at the following two codes

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

All add attributes to the 'window' object, one can be deleted, and the other cannot be deleted. However, adding 'var' can be scope-related. Without adding 'var', you always dynamically add attributes to the 'window' object. The following code is proof

var test = function(){
 a = 'aa';
}
test();
alert();//pop up'aa' 

Since the window object is a global object, it can be added by default. The following paragraph has the same effect.

var test = function(){
 a = 'aa';
}
test();
alert(a);//pop up'aa' 

Speaking of this, students who are thinking seriously must have a question now, why can implicitly declared global variables be deleted, but can't be deleted explicitly declared global variables?

The reason is that "delete cannot delete attributes with configurability false". The properties of some built-in objects are not configurable, such as properties of global objects created through variable declarations or function declarations. The following code is proof.

delete ; // false cannot be deleted, this property is not configurablevar a = 'aa';
delete ;//false cannot be deleted, this property is not configurablefunction test(){};
delete ;//false Not deleteable,This property is not configurable 

Then you will understand that the global variable declared through 'var' actually adds an unconfigurable property to the 'window' object, and does not add a global variable declared by 'var' actually adds a configurable property to the 'window' object.

Note that the above use window can be replaced by this window, such as:

var test = function(){
 a = 'aa';
}
test();
alert();//pop up'aa' 

As for the reasons, please check out the article I wrote before'this, this, discuss this in javascript again, super comprehensive'

Below, the var keyword in javascript will be drawn and explained separately to you.

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

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

The result is: undefined

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

The result is: 1

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

The result is: undefined

var01 = 1;
function funtest() {
 (var01);
 var01 = 0;
}

The result is: 1

After seeing this, you know more about var in JavaScript. I believe that you will gain more or less through this article. For more information about javascript var, please continue to pay attention to this site, thank you!