JavaScript variable declaration has the difference between Var and no Var

Source: Internet
Author: User

1. Variables defined with var within the scope of the function are local variables, which are defined as global variables without var.
Using the VAR definition
var a = ' Hello World ';
function bb () {
var a = ' Hello Bill ';
Console.log (a);
}
BB ()//' Hello Bill '
Console.log (a); ' Hello World '

Do not use VAR definitions
var e = ' Hello World ';
function cc () {
e = ' Hello Bill ';
Console.log (e); ' Hello Bill '
}
CC ()//' Hello Bill '
Console.log (e)//' Hello Bill '

2. Under global scope, variables defined with Var cannot be deleted, and variables with no Var defined can delete. It also implies that the global variable is strictly not a real variable, but rather a property of the global object, because the property can be removed by the delete and the variable is not available.


3. Using var to define variables also promotes variable declarations, which are
Use Var to define:
function hh () {
Console.log (a);
var a = ' Hello World ';
}
HH ()//undefined

Do not use VAR definitions:
function hh () {
Console.log (a);
A = ' Hello World ';
}
HH ()//' A is not defined '

This is the declaration of variables defined using VAR in advance.
4. In ES5 ' use strict ' mode, if the variable is not defined with VAR, it will be an error.

JavaScript variable declaration has the difference between Var and no Var

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.