0

Below is from MDN "Function Expression". Shouldn't typeof baz be function, not undefined? Why is bar === baz false?

var foo = function() {}
foo.name // "foo"

var foo2 = foo
foo2.name // "foo"

var bar = function baz() {}
bar.name // "baz"

console.log(foo === foo2); // true
console.log(typeof baz); // undefined
console.log(bar === baz); // false (errors because baz == undefined)
Barmar
  • 741,623
  • 53
  • 500
  • 612
talktame81
  • 23
  • 4

1 Answers1

0

You’re mixing function declaration styles.

If there’s a var, the line will get interpreted at runtime. If it’s function name() it will get interpreted at compile time.

If you mix it, you’ll get the function expression (hoisting) instead of the function declaration. And so you get bar instead of baz

Valentine Bondar
  • 139
  • 2
  • 10