Javascript variables and their scopes and early declarations, javascript Variables

Source: Internet
Author: User

Javascript variables and their scopes and early declarations, javascript Variables

Variables and their scopes in javascript are the key points in the foundation. After reading the JavaScript authoritative guide, we will summarize:


If the initial value is not specified for the variable in the var declaration statement, although this variable is declared, its initial value is undefined before it is saved to a value.

(If a variable is declared in a scope, it is also undefined before the value is assigned)

The function scope of JavaScript indicates that all variables declared within the function are always visible in the function body. Interestingly, this means that the variable is even available before it is declared. This feature of JavaScript is informal referred to as hoisting. That is, all variables declared in JavaScript Functions (but not related to assignment) are "advanced" to the top of the function body.

The "advance declaration" operation is performed during "pre-compilation" of the JavaScript engine. Before the code starts to run, please read the related ppt for more details:

Http://www.slideshare.net/lijing00333/javascript-engine (to wall)

Baidu cloud disk link: http://pan.baidu.com/s/1mguZ1OG

For example:

Var scope = 'global ';

Debugger;

Function F (){

Debugger;

Alert (scope); // undefined

Var scope = 'local'; // The variable assigns an initial value here, but the variable itself is defined anywhere in the function.

Alert (scope); // local

Alert (alias); // alias is not defined (undefined)

}

F ();

 

That is:

Function (){

Alert (scope );

Var scope = 'local ';

Alert (scope );

}

It can be understood:

Function (){

Var scope;

Alert (scope );

Scope = 'local ';

Alert (scope );

}

In addition, I highly recommend the JavaScript authoritative guide, especially a book that many web experts love.

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.