A study on the difference between a JavaScript-defined variable with VAR and var _javascript tips

Source: Internet
Author: User

In this paper, we analyze the difference between the Var and the variable with the JavaScript definition variables. Share to everyone for your reference. The specific analysis is as follows:

Look directly at the example to illustrate:

Copy Code code as follows:
<script language= "javascript" type= "Text/javascript" >
var abc=89;//with Var, representing global variables
function Test () {
var abc=80;//within a function, if not Var, means using a global variable outside the function; Var, which represents a new definition of a global variable
}
Test ();
Window.alert (ABC);
</script>

Strictly speaking: The function body does not take the Var, does not mean defines a variable, but carries on the variable assignment value, namely Var abc;abc=8. If you are assigning abc=80 (without Var) in the body of the function, the real process is this--the statement first finds the variable ABC in the body of the function, and if it doesn't, it goes up to look for the variable ABC outside the function body, and if it's still not there, there's no way to define the variable var ABC in the function body.

So, why

Copy Code code as follows:
function Test () {
ABC = 80;
}

The ability to call variable ABC directly outside of a function is one such reason.

I hope this article will help you with your JavaScript programming.

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.