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
  • 2,099
  • 6
  • 26
  • 50
  • It's very useful when you are debugging it since the stacktrace will list the function name instead of "anonymous". – silvio 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
  • 376,160
  • 25
  • 347
  • 392