DEV Community

Discussion on: Anonymous Functions vs Named Functions vs Arrow Functions

Collapse
 
mail2lawi profile image
mail2lawi

Thanks so much for this guide. Very helpful for a beginner like I.

I have a few questions though.

  1. I found wording is a bit confusing in the last 2 code snippets. You are converting a function named brag to an anonymous function yet from the code it looks like the function was anonymous to begin with.

  2. And the one that I'm more curious about, is there a way to do the same conversion but using an arrow function syntax instead? I tried it and keep getting the SyntaxError: missing ) after argument list

// Arrow function works ok
var brag = (count) => {
     return ("Arrow: I can do " + count + " pushups")
}
console.log(brag (30)) // Arrow: I can do 30 pushups

// Embedded into console.log fails
console.log((count) => {
     return ("Arrow: I can do " + count + " pushups")
} (30)) //SyntaxError: missing ) after argument list
Enter fullscreen mode Exit fullscreen mode

Is there a rule against this kind of convention?

Collapse
 
mathlete profile image
Analogy | Absence | Example

@mail2lawi You are 100% correct! I meant to write a named function, but I wrote an anonymous function instead. I have now corrected it, thanks you you!

Collapse
 
badgamerbad profile image
Vipul Dessai

here the IIFE syntax is incorrect, i.e. the round brackets around the arrow function are missing

// Embedded into console.log fails
console.log(((count) => {
     return ("Arrow: I can do " + count + " pushups")
})(30)) // Arrow: I can do 30 pushups
Enter fullscreen mode Exit fullscreen mode