Home > Web Front-end > JS Tutorial > Function declarations take precedence over variable declarations in JavaScript

Function declarations take precedence over variable declarations in JavaScript

高洛峰
Release: 2016-11-26 09:16:21
Original
1127 people have browsed it

For the same identifier, declare it with var and function. What is it in the end?
var a; // Declare a variable with the identifier a
function a() { // Declare a function with the identifier a
}
alert(typeof a);
displays "function", that is, function has a higher priority 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.
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 at the same time when declaring a.
function a() {
}
var a = 1; // Note here
alert(typeof a);

At this time, "number" is displayed but not "function", which is equivalent to
function a( ) {
}
var a;
a = 1; // Note here that
alert(typeof a);
means "var a = 1" is split into two steps. a has been reassigned, naturally it is the last value.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template