https://bigfrontend.dev/quiz/function
What does the code snippet below output by console.log
?
function foo() {
console.log(1);
}
var foo = 2;
function foo() {
console.log(3);
}
foo();
Error
Before code execution, function and var
variable declarations are hoisted to the top of the scope in which they are declared. JavaScript first moves function declarations, then var
variable declarations. The order of declarations doesn't matter. When a var
variable and a function have the same name, the var
variable declaration overrides the function declaration, thus the code snippet above ends up in error: "foo is not a function".