Variable issues in JavaScript Functions

Source: Internet
Author: User

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>
<Body>
<SCRIPT type = "text/JavaScript">
Testvar1 ();
Testvar ();
Alert (TEST); // output hello!
Alert (test1); // an error is reported in the output. test1 is not defined.
Function testvar1 ()
{
VaR test1 = "hello1! ";
}
Function testvar ()
{
Test = "Hello! ";
}
</SCRIPT>
</Body>
</Html>

Analyze the variables in JavaScript Functions through the instance program, without the difference between var declaration and VAR declaration.
Conclusion:

1. variables declared by VAR are equivalent to global variables in JavaScript Functions, that is, they are visible outside JavaScript functions. However, before using this variable, you must call the corresponding JavaScript function.

2. the variables declared by VAR in JavaScript Functions are equivalent to local variables, and their visibility is only within the corresponding JavaScript function.

3. We recommend that you display the definition variables before use, so that the program can be readable and maintainability. Try not to use the method in the first 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.