Differences between Javascript global variables var and non-var

Source: Internet
Author: User
This article mainly introduces the differences between Javascript global variables var and non-var. For more information, see global variables, variables defined in the form of a = 1 in the function scope will be a global variable. In the global scope, you can create globally visible names in the following three forms:

The Code is as follows:


Script
Var a = 1;
B = 2;
Window. c = 3;
Script


For B = 2, it is actually the same as c. When executing this value assignment statement, it will search for a variable named B along the scope chain, the top of the scope chain has not been found, so add a property B to window and assign a value.

There are two differences between var and non-var:

1. The global variables of var cannot be deleted because delete intelligently deletes the deletable attributes of an object, and the global attributes defined by var are marked as non-deletable. Note that if the delete operation fails, the system will not throw an error. The return value of the delete operation is true | false.

2. The global variables defined by var are upgraded, but those defined by var are not. You can see the execution result of the following program:

The Code is as follows:


Script
Alert ();
Var a = 1;
Script


The Code is 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.