In JavaScript, function declaration takes precedence over variable declaration instance analysis _ javascript skills

Source: Internet
Author: User
The same identifier is declared with var and function successively. What is it? The Code is as follows:


Var a; // declare a variable with the identifier
Function a () {// declare a function. The identifier is.
}
Alert (typeof );


"Function" is displayed, that is, the priority of a function is higher than that of var.
Some people think this is the reason for sequential code execution, that is, a is overwritten by the funcion executed later. Okay. change them.

The Code is as follows:


Function (){
}
Var;
Alert (typeof );



The result still shows "function" instead of "undefined ". That is, function declaration takes precedence over variable declaration.
We make a slight modification to the code and assign values when declaring.

The Code is as follows:


Function (){
}
Var a = 1; // note that
Alert (typeof );



"Number" is displayed but not "function", which is equivalent

The Code is as follows:


Function (){
}
Var;
A = 1; // note that
Alert (typeof );


Split "var a = 1" into two steps. A is re-assigned, which is the final value.
Related 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.