The difference between "Var" and "Var" added in JS

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.

1. Add Var, which is a local variable within the method, and a global variable outside the method.

2. No Var, all global variables.

3. Within the method, add var as a local variable, and no var is a global variable (after executing the current method).



var num1 = 1;

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

and num2 = 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.

http://blog.csdn.net/charlene0824/article/details/52252824--A detailed list of scopes in JavaScript

In a Web browser, the global execution environment is considered a Window object, so all global variables and functions are created as properties and methods of the Window object.

So num1 and num2 are all properties of the Window object, except that variables declared without var can be deleted.

In the ECMASCRIPT5 standard, there is a "strict pattern" (Strict mode). In strict mode, assigning a value to an undeclared identifier throws a reference error, thus preventing accidental creation of global variable properties. Some new versions of browsers are now supported.

The difference between "Var" and "Var" added in JS

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.