DEV Community

Cover image for What is JavaScript's Anonymous and Arrow function?

What is JavaScript's Anonymous and Arrow function?

Suraj Vishwakarma on July 18, 2022

Introduction William Shakespeare once said that “What’s in Name?” but as a developer, we can disagree. We have to name variables, classe...
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

A lot of confusion and incorrect information in this article. Many of your 'anonymous' functions are not anonymous functions at all

Collapse
 
surajondev profile image
Suraj Vishwakarma

If you read the article, it's for storing the function in a variable.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

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 expression
console.log( (function test() { console.log('hello') }).name )  // 'test'

// it can be invoked immediately
(function test() { console.log('hello') })()  // 'hello'
Enter fullscreen mode Exit fullscreen mode

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.

Thread Thread
 
surajondev profile image
Suraj Vishwakarma

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.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

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:

const notAnonymous = function () { return 1 }
console.log(notAnonymous.name)  // 'notAnonymous'
Enter fullscreen mode Exit fullscreen mode

Contrast this with an actual anonymous function:

console.log( (function() { return 1 }).name )  // ''
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
surajondev profile image
Suraj Vishwakarma

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.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Again, this isn't really related to anonymous functions. To use ANY function (anonymous or otherwise) later - we need to store a reference to it somewhere - it doesn't matter if it is named or not.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

It's also perfectly possible to store anonymous functions without them acquiring a name (so they retain their anonymity) - for example in an array:

const a = []
a.push(function() { console.log('hello') })
console.log(a[0].name)  // ''
a[0]() // 'hello'
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
surajondev profile image
Suraj Vishwakarma

It retains anonymity but still, you need to store it into a variable.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

As the Anonymous function does not have a name, we can pass directly as the arguments to other functions

Not having a name has no bearing on this. It's equally possible to pass in a function expression that does have a name. This may actually be preferable sometimes as it may aid the reading of debug traces in some environments (where the name will be used for display purposes).

setTimeout(function myTimeOutFunction() {
     console.log(1 sec has passed)  
}, 1000)
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
surajondev profile image
Suraj Vishwakarma

Definitely you just need to pass function it can be annonymous, named or arrow. But annonymous will be easy to implement. It will be mostly one time function, you can go with functions not having name.

Collapse
 
jonrandy profile image
Jon Randy 🎖️
setTimeout(myFunction() {
     conole.log(1 minute has passed)  
}, 1000)
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
surajondev profile image
Suraj Vishwakarma

Typo fixed.