The difference between adding var when defining variables in javascript and without var is that using criptvar

Source: Internet
Author: User

The difference between adding var when defining variables in javascript and without var is that using criptvar

1. The external is global, and the internal is local variables.

2. Add var as a local variable (in the method) without var as a global variable (once used in the method)

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var golbe = "global ";
Test ();
Function test (){
Var local = "local ";
Document. write (golbe );
Document. write (local );
}
Document. write (golbe );
Document. write (local );
</Script>

In the preceding test method, after the var of the local variable is removed, the local variable becomes a global variable. However, if the local variable is not used locally, the local variable as a global variable is invalid.

To verify this, I commented out the code that only uses the local variable in the test method and found that the Code cannot be printed externally.

Summary: global variables do not have to declare var in the var function. Adding or not adding the var keyword when defining global variables does not have any impact; however, if the var keyword is not added when defining a local variable, the javascript interpreter will interpret it as a global variable.

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.