Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 752 Bytes

70.function.md

File metadata and controls

34 lines (23 loc) · 752 Bytes

70. function

Problem

https://bigfrontend.dev/quiz/function

Problem Description

What does the code snippet below output by console.log?

function foo() {
  console.log(1);
}
var foo = 2;
function foo() {
  console.log(3);
}
foo();

Answer

Error

Explanation

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".