JavaScript global variable var vs. var difference deep Parsing _ Basics

Source: Internet
Author: User

I believe you are not unfamiliar with global variables, in the function scope of the a=1 this form of the variable is defined as a global variable, in the global scope, in the following 3 forms can be created on the global visible name:

Copy Code code as follows:

<script>
var a = 1;
b = 2;
WINDOW.C = 3;
</script>

For b=2 this way, it is actually the same as C, when executing this assignment statement, it follows the scope chain to find the variable named B, and finds that the top of the scope chain has not been found yet, and then adds a property B to the window and assigns the value.

There are two differences between Var and no Var:

The global variable of 1 var cannot be deleted because the Delete smart Delete object's removable properties, and the global properties defined by VAR are marked as not to be deleted. It should be explained that delete is unsuccessful and will not be thrown incorrectly, and the return value of delete is True|false.

2 The global variables defined by VAR are promoted, and global variables defined without VAR are not promoted. You can see the results of the following program:

Copy Code code as follows:

<script>
alert (a);
var a=1;
</script>

Copy Code code as follows:

<script>
alert (a);//error, a undefined
A=1;
</script>

Related Article

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.