2. variable var keyword

Source: Internet
Author: User

When we look at the JS declaration variable, we often find that there are some variables before the Var but then not, then what is the difference?

If this happens in a function , the variable that is defined by VAR is a local variable, which is defined as a global variable without var.

// using Var var y= "dsh"function  Test () {      var y= "WX";} test (); Console.log ( y);

[Web browser] "DSH"

// do not use Var var y= "dsh"function  Test () {     y= "wx";} test (); Console.log (y) ;

[Web browser] "WX"

Under global scope, variables defined with var cannot be delete, and variables defined without var can be delete.
This means that the implied global variable is strictly not a real variable, but rather a property of the global object, because the property can be deleted by the delete and the variable is not available.

var x= "Dsh";      Y= "wx"; Delete x; Delete Y;console.log (x); Console.log (y);

[Web browser] "DSH"
[Web browser] "Uncaught referenceerror:y is not defined"

Using var to define variables also promotes variable declarations, but variables without var do not

Console.log (x); // the lifting variable x is undefinedconsole.log (y); // not promoted, y is not defined. Y is not definedvar x= "Dsh";    Y= "wx";

[Web browser] "Undefined"
[Web browser] "Uncaught referenceerror:y is not defined"

In ES5 ' use strict ' mode, if the variable is not defined with VAR, it will be an error.

2. variable var keyword

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.