What's the difference between named functions and arrow functions in JavaScript?
Posted by ketralnis@reddit | programming | View on Reddit | 12 comments
saantonandre@reddit
Fuck me, an original looking blog that does not look the same as every other gpt spam blogs, and somehow is about programming??? in this econ
How do you manage
butt_fun@reddit
This sub went to shit after the mods left a few years ago
This type of post would have historically been removed because it's Fisher Price My First JavaScript Knowledge and we got about five posts a month about this topic. Whereas today, it's one of the better posts
geowarin@reddit
Unless it is a one-liner, arrow function are not more concise than function declaration:
butt_fun@reddit
I think you mean "unless it's anonymous"
Naming it is the part that's longer with arrow function than "normal" functions
Smooth_Detective@reddit
Arrows are bit more functional in what they convey as well. If you're assigning something to a variable (be that a function) you likely intend to pass it someplace else as opposed to a standard function which is intended to be called.
ejfrodo@reddit
This is all technically correct info but this stuck out as kind of an odd stance to take
I think most would agree that the main difference is they don't create their own lexical scope and inherit their
this
context from the scope they're defined in. That and they can't be used with thenew
operator. The syntax is more just a means to an end.zhivago@reddit
Arrow functions create their own lexical scope.
this isn't inherited, but rather is bound into the lexical closure of the arrow function.
damnNamesAreTaken@reddit
So, I'm asking this as someone who rarely touches JavaScript but occasionally had to read/write a bit. After reading this article I'm still left with the question of, other than being more compact, what is the advantage of the arrow style functions? To put it another way, why wouldn't I just use the other style everywhere?
devbnk@reddit
Differences Between Arrow and Regular Functions
damnNamesAreTaken@reddit
Thanks. This actually clarified the difference for me.
modernkennnern@reddit
Unless you use the
this
keyword, there's no difference.Advantages of
function
:They're hoisted, so ordering doesn't matter (functions can be called before they're declared)
Multiline functions require fewer total characters
Advantages of arrow functions:
More sane when it comes to
this
.One-liners are shorter.
Other than that there's basically no difference and just personal preference.
gareththegeek@reddit
The difference is this