JavaScript declares global variables three ways of difference

Source: Internet
Author: User

In JavaScript, declare variable format: var (keyword) + variable name (identifier).
Mode 1
var test;
var test = 5;

Note that the sentence cannot be contained within a function, otherwise it is a local variable. This is the first way to declare a global variable.

Mode 2
Test = 5;

No var is used to assign a value directly to the identifier test, which implicitly declares the global variable test. Even though the statement is within a function, when the function is executed, test becomes a global variable.

Mode 3
Window.test;
Window.test = 5;

This approach is often used when an anonymous function is executed to expose some functions to the global. As the last sentence in JQuery1.5
Window.jquery = window.$ = JQuery;

Directly in the JS file to write the Var variable =xxx on the line, so all JS can get this variable, or directly write window.xxxx=xxx

JavaScript declares global variables three ways of difference

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.