Named function can’t be invoked immediately after declaration
This is not correct - you can declare a named function without a function statement, and immediately invoke it:
// this is a named function in a function expressionconsole.log((functiontest(){console.log('hello')}).name)// 'test'// it can be invoked immediately(functiontest(){console.log('hello')})()// 'hello'
It is true that you cannot immediately invoke a named function created with a function statement, but not true that you cannot invoke a named function immediately after declaration.
Immediate invocation is possible because the preceding code is an expression whose value is a function - it has nothing to do with whether the function is anonymous or not. If you have defined a function using a function statement - then that statement is not an expression and therefore has no value that you can call.
If you read the article, it's for storing the function in a variable.
Yes, but the very act of assignment makes those functions cease to be anonymous functions. They acquire the name of the variable/constant. This can be shown by checking the function's name:
After the initial declaration of the anonymous function, it can't be invoked later. For that, we have to store it inside a variable. That's I did above. This has led to having a name.
There are a number of mistakes in this example. Firstly - it won't run because myFunction() should say just function or function myFunction(). Further, console is misspelled and the message will display after 1 second, not one minute.
A lot of confusion and incorrect information in this article. Many of your 'anonymous' functions are not anonymous functions at all
If you read the article, it's for storing the function in a variable.
This is not correct - you can declare a named function without a function statement, and immediately invoke it:
It is true that you cannot immediately invoke a named function created with a function statement, but not true that you cannot invoke a named function immediately after declaration.
Immediate invocation is possible because the preceding code is an expression whose value is a function - it has nothing to do with whether the function is anonymous or not. If you have defined a function using a function statement - then that statement is not an expression and therefore has no value that you can call.
Considering this, I like to thank you as I was not having the knowledge on this. I have updated the article as per your feedback.
Yes, but the very act of assignment makes those functions cease to be anonymous functions. They acquire the name of the variable/constant. This can be shown by checking the function's name:
Contrast this with an actual anonymous function:
After the initial declaration of the anonymous function, it can't be invoked later. For that, we have to store it inside a variable. That's I did above. This has led to having a name.
There are a number of mistakes in this example. Firstly - it won't run because
myFunction()should say justfunctionorfunction myFunction(). Further,consoleis misspelled and the message will display after 1 second, not one minute.Typo fixed.