In JavaScript, function declaration takes precedence over variable declaration.

Source: Internet
Author: User

The same identifier is declared with VAR and function successively. What is it?

VaR A; // declare a variable with the identifier afunction A () {// declare a function with the identifier a} alert (typeof );

"Function" is displayed, that is, the priority of a function is higher than that of var.

Some people think this isCodeThe reason for sequential execution is that A is overwritten by the funcion executed later. Okay. change them.

Function A () {} var A; 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.

Function A () {} var A = 1; // note alert (typeof A) here );

"Number" is displayed but not "function. If you place the VaR declaration statement at the top, the result is the same.

VaR A = 1; // note that function a () {} alert (typeof );

 

This is equivalent

Function A () {} var A; A = 1; // note alert (typeof A) here );

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.