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)

2
  • baz is the name of the function (Google "Named function") but on the function itself. Commented Nov 17, 2020 at 21:25
  • 1
    The scope of the name in a named function expression is just the function body. Commented Nov 17, 2020 at 21:26

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.