Example analysis of a function declaration prior to a variable declaration in JavaScript _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

var A; Declare a variable with an identifier of a
function A () {//Declaration of functions, identifiers also for a
}
Alert (typeof a);

Displays "function", which means that the function has precedence over var.
Some people think that this is the reason for the sequential execution of code, that is, a is covered by the funcion of execution. Well, switch them.
Copy Code code as follows:

function A () {
}
var A;
Alert (typeof a);


The results still show "function" rather than "undefined". That is, the function declaration takes precedence over the variable declaration.
We modify the code slightly and declare a while assigning a value.
Copy Code code as follows:

function A () {
}
var a = 1; Note here
Alert (typeof a);


This shows "number" but not "function", which is equivalent
Copy Code code as follows:

function A () {
}
var A;
A = 1; Note here
Alert (typeof a);

That is, "var a = 1" split into two steps. A is assigned a value, and nature is the last one.

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.