Home >Web Front-end >JS Tutorial >Example analysis of function declaration taking precedence over variable declaration in JavaScript_javascript skills

Example analysis of function declaration taking precedence over variable declaration in JavaScript_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:55:321323browse
Copy code The code is as follows:

var a; // Declare a variable with identifier a
function a() { // Declare a function, the identifier is also a
}
alert(typeof a);

displays "function", which is the priority of function Level higher than var.
Some people think this is the reason why the code is executed sequentially, that is, a is overwritten by the funcion executed later. Okay, swap them around.
Copy code The code is as follows:

function a() {
}
var a;
alert(typeof a);


The result still shows "function" instead of "undefined". That is, function declarations take precedence over variable declarations.
We slightly modify the code and assign a value when declaring a.
Copy code The code is as follows:

function a() {
}
var a = 1; // Note here
alert(typeof a);


At this time, "number" is displayed but not "function", which is equivalent to
Copy code The code is as follows:

function a() {
}
var a;
a = 1; // Note here
alert(typeof a);

means "var a = 1" is split into two steps. a has been reassigned, naturally it is the last value.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn