DEV Community

Takuya Kikuchi
Takuya Kikuchi

Posted on

Should I use Arrow Function? or Regular Function? ๐Ÿค”

My conclusion

I go with Arrow Function(most of the cases)

Reasons

Arrow function can be less bug-prone

  • It does't allow duplicate argument names
  • No hoisting with using const
  • It cannot define with the same function name with using const

When to use Regular function

  • We we want to leverage the difference of this behavior for regular function
  • In typescript, when you want to use Function Overloads (Thanks for your comments, @lexlohr ๐Ÿ˜‰)

Difference in details

There are other differences between Arrow and Regular functions as well. And following reference explains very well on that with good example codes ๐Ÿ‘

Reference

Discussion (3)

Collapse
lexlohr profile image
Alex Lohr

In typescript, there is another reason to go with classic functions: if you want to overload them. Otherwise, using arrow functions is a good choice.

Collapse
takuyakikuchi profile image
Takuya Kikuchi Author • Edited

Thanks for your comment, Alex!

Great point! Let me add this point to the article ๐Ÿ‘

TIL: TypeScript: Documentation - More on Functions #function-overloads

Collapse
takuyakikuchi profile image
Takuya Kikuchi Author

Related to this topic, Kent C. Dodds introducing his loose rules. It is worth a read ๐Ÿ˜‰
Function forms