DEV Community

Cover image for why you shouldn't use arrow functions?

why you shouldn't use arrow functions?

Blessing Hirwa on December 24, 2020

Who doesn’t love the simplicity of arrow functions? Introduced as part of the ECMAScript 6, arrow functions went viral. The new syntax to declare f...
Collapse
 
saba1121 profile image
Saba1121

const holidays = () => return "Merry Christmas and a happy new year 😀!"

In this example you dont really need word "return". Arrow functions which are one line and dont use curly braces automatically return result. So it should be like this:

const holidays = () => "Merry Christmas and a happy new year 😀!"

Collapse
 
blessinghirwa profile image
Blessing Hirwa

I wanted to be more clear. But yours also works well.

Collapse
 
lucgauer profile image
Lucas Gauer • Edited

Usage of return explicitly into a single statement arrow function (without the curly brackets), is actually a syntax error 🙁.

Collapse
 
blessinghirwa profile image
Blessing Hirwa

I also changed it lol.

Collapse
 
baenencalin profile image
Calin Baenen

Actually, you said arrow functions don't have arguments.
That is false.

const add = (...nums) => {let i = 0; for(let num of nums) i += num; return i;}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
blessinghirwa profile image
Blessing Hirwa

You can read it from here:
developer.mozilla.org/en-US/docs/W...

Collapse
 
baenencalin profile image
Calin Baenen

Oh, you meant the keyword (you should probably specify that in the post).

Thread Thread
 
blessinghirwa profile image
Blessing Hirwa

Yeah, I just added it now.

Collapse
 
chrisburkssn profile image
Chris Burks

Thanks for the info. Looking for the continuation.
Small note: I think there is a typo for your first example Object methods. You mention "the property post.claps would increase by one". However, there is not property "claps" in the code example. There is "likes" though.

At any rate, good stuff here and thanks again for posting.

Collapse
 
blessinghirwa profile image
Blessing Hirwa

ohhh I didn't catch that. Thanks for correcting

Collapse
 
matjones profile image
Mat Jones

As far as your first point, that's literally just syntax sugar/shorthand for a regular function declaration.

Collapse
 
tonyscruze profile image
TonySCruze

Great article. Thanks 🙂

Collapse
 
lyrod profile image
Lyrod

For object methods, you should use the "like() {}" syntax

Collapse
 
blessinghirwa profile image
Blessing Hirwa

That's a good one.

Collapse
 
oreste profile image
Oreste Abizera

cool stuff! Actually, I didn't know this 😍(before reading this post)

Collapse
 
blessinghirwa profile image
Blessing Hirwa

Glad it helped 😁

Collapse
 
hasnaindev profile image
Muhammad Hasnain

Please replace "why" with "when" in your post as it is very misleading, thanks.