0

Why does the first snippet print 'bar' and the second ''?

const bar = () => {}
console.log(bar.name) // 'bar'

const bar = 1 && (() => {})
console.log(bar.name) // ''

I also note that the same behavior occurs with traditional (ie. non-arrow) functions.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • `function.name` only works for unnamed functions when they are directly assigned to a property or variable. – Ruan Mendes Oct 07 '22 at 16:38
  • 3
    For name inference to occur, the function expression has to be directly assigned to the `const`. When it becomes indirect (in your case, because it's the operand to `&&`), the inference doesn't occur. – T.J. Crowder Oct 07 '22 at 16:38
  • 2
    I'm not getting the reason for the downvotes. This isn't obvious and is tricky to research. – T.J. Crowder Oct 07 '22 at 16:43
  • I think reading more about the logical AND assignment operator (&&=) would help this be a little clearer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND_assignment – Jeremy Harris Oct 07 '22 at 16:43
  • `&&=` is not used in this question. – Ben Aston Oct 07 '22 at 16:53

0 Answers0