Priority of variable function declaration in Javascript

Source: Internet
Author: User
Case 1 console. log (a); Output functiona () {} functiona () {}; vara3; Case 2 functiona () {}; vara3; console. log (a); Output 3. Why are the two outputs different? Resolution: for Case 1, the execution process can be subdivided into 1. First, a space will be applied to declare function a, and then the variable a will be executed.

Console. log (a); // output function a () {} function a () {}; var a = 3;

Case 2

Function a () {}; var a = 3; console. log (a); // output 3


Why are the two outputs different?

Resolution:

Case 1

The execution process can be subdivided

1. First, I will apply for a space, start to declare function a, and then execute the Declaration variable a, and find that the declaration of a already exists, so I will not declare it.

2. Then, execute console. log (a); that is, the output function.

3. Finally, execute the value assignment operation to convert a to 3. At this time, function a is converted to the variable type and assigned a value of 3. For verification, you can change the program to the following

a();function a() {console.log(5);};var a=3;a();

The program execution result is as follows:

Supplement

1. the following code execution result is:

var x=0;f();console.log(x);var f=function(){x=1;}f();console.log(x);function f(){x=2;}f();console.log(x);


Answer: 2 1 1

The above is the priority of variable function declaration in Javascript. For more information, see PHP Chinese website (www.php1.cn )!

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.