0

How is the output for below snippets - function.

console.log(typeof foo);

function foo() {
  return "bar";
}
var foo = "bar";
function foo() {
  return "bar";
}
var foo;

console.log(typeof foo);

Shouldn't it be undefined due to var foo?

1 Answers1

1

var foo redundantly declares the variable and doesn't assign anything to it.

It doesn't explicitly assign undefined so it doesn't overwrite the existing function value with undefined.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • also have a look at this question that has been answered https://stackoverflow.com/questions/15057649/function-and-variable-with-the-same-name – nontechguy Sep 14 '21 at 09:15