The difference between "Var" and "Var" when declaring in JavaScript

Source: Internet
Author: User

While JavaScript declares variables, while declaring them with the Var keyword and declaring them without the keyword, there are many times when it is not a problem to run, but there is a difference between the two ways. Code that works correctly does not represent the appropriate code.

var num = 1;

is to declare the variable in the current domain. A local variable (local variable) if declared in the method, or a global variable if declared in the global domain.

and num = 1;

In fact, it is a property assignment operation. First, it tries to chain in the current scope (as declared in the method, then the current scope chain represents the global scope and the method local scope etc ... ), if NUM is found in any current scope chain, the NUM attribute is executed, and if NUM is not found, it will create the NUM attribute and assign a value in the global object (that is, the topmost object of 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 hard for you to understand the difference between a "variable declaration" and a "Create object property" here. In fact, JavaScript's variable declarations, creation properties, and each of the properties in each JavaScript have a certain flag stating their properties----such as read-only (ReadOnly) non-enumerable (Dontenum) non-deleting (dontdelete), and so on.

Since the variable declaration comes with a non-deleted attribute, the comparison of var num = 1 with num = 1, which is a variable declaration with a non-deleted attribute and cannot be deleted, is a property of the global variable and can therefore be removed from the global variable.

Let's look at the example below:

When JavaScript declares variables

var AAA = 111;

And

AAA = 111;

Is it the same in two different ways?

Say less nonsense, first on the code.

var AAA = 11;function test4 () {    var aaa = 22;} Test4 (); Console.log (AAA);

What was the result? 11

This is a good understanding that the Var AAA declaration inside the function is an internal variable, and the result is the value of the first AAA.

Change as follows:

var abc = 11;function test4 () {    abc = 22;} Test4 (); Console.log (ABC);

What was the result? 22

Further changes:

function test4 () {    var aaa = 22;} Test4 (); Console.log (AAA);

What's the result? Run an Error! REFERENCEERROR:AAA is not defined!

Change:

function test4 () {    var aaa = 22;} Test4 (); Console.log (TEST4.AAA);

Operation will not error, the output is undefined.

Conclusion 1: Variables declared within a function or object construct are private. cannot be accessed externally. Includes the object after the prototype inherits.

But if so:

function test4 () {    BBB = 33;} Test4 (); Console.log (BBB);

The result is 33.

Point solution?

This is the difference between a declaration with Var and no var.

Conclusion 2: No Var is assigned within a function or construct, and the variable bbb is searched from within the function to the top layer. Declare a var BBB on the top level;

It's horrible. If a big project changes the value of BBB here, and does not add Var happens throughout the project global variable has a same name BBB is changed, no var is not only function in this function or object. It's hard to find out the mistake.

So write code must be cautious. Declaring variables to add can not be afraid of trouble. The result is completely different.

The difference between "Var" and "Var" when declaring in JavaScript

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.