Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

What happens when you declare the same function twice?

Do you know what will be the output of this code?

JavaScript
function f() {
  return 1;
}

function f() {
  return 2;
}

console.log(f());
Show answer and explanation

The answer is 2.

Both declarations are hoisted before any code runs, and the later one replaces the earlier one. With let or const this would be a SyntaxError.

Take the full quiz Next question →

More Scope & Hoisting questions

  1. How do closures create private variables in JavaScript?
  2. How do you write a once() function with a closure?
  3. Can you call a function before it is declared in JavaScript?
  4. Why can't you call a function expression before it's defined?
  5. Can you change this on a bound function with call()?

Related topics