2

Consider the following snippet:

let myFunc = function foo() {
   console.log('whatever');
}

myFunc(); // 'whatever'
foo();    // ReferenceError

What reason is there to give this function a name if you can't use it?

Steverino
  • 1,845
  • 3
  • 21
  • 42
  • It's very useful when you are debugging it since the stacktrace will list the function name instead of "anonymous". – WebDever Feb 09 '19 at 22:04

1 Answers1

0

A named function is to call itself with this given name. The name can not changed later.

For example this function is calling itself only two times.

let myFunc = function foo() {
   console.log('whatever');
   foo.count = (foo.count || 0) + 1;
   if (foo.count < 3) foo();
}

myFunc();
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358