+ 3

I don't see the beauty of ES6 arrow functions

Hello guys i don't know if it's really awkward but the arrow function feel so intimidating and strange to me , i've been trying to understand it for over a week or so , can anyone explane it more clearly ?

26th Nov 2018, 12:13 PM
Omar khaled
Omar khaled - avatar
9 Answers
26th Nov 2018, 3:35 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 8
A normal JavaScript function: function addition(a, b) { return a + b; } It could be written in variable function below: var addition = function (a, b) { return a + b; If written in ES6, arrow function: var addition = (a, b) => { return a + b; } For single statement return arrow function, It could also be written in shorthand below: var addition = (a, b) => a+b;
26th Nov 2018, 12:27 PM
CalviŐ˛
CalviŐ˛ - avatar
+ 6
Omar khaled Here an example, to love arrow functions! 😀👍😉 https://code.sololearn.com/WxEg6de0NtKy/?ref=app
26th Nov 2018, 4:54 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 5
Declaring a function in a variable has advantages, and it is called a function expression (opposed to a function declaration). This type of function doesnt get invoked when the code is read and only when it is called, and it is very useful when using first class functions (passing a function as a parameter to another function), as well as other advantages.
26th Nov 2018, 6:49 PM
Foobatboy
Foobatboy - avatar
+ 4
I saw a presentation once where the speaker said arrow functions were originally used to make presentations more clear. It was deadpanned (so, no idea if it was a joke; I don't remember if anyone laughed) but it felt like the tone was "because it's less clutter". I think I can find the presentation again (it was a really interesting list of JS feature quirks...though now...I think it might contain a significant amount of adult language), but anyway..just earmarking this Q&A so I have a bookmark for possible update.
26th Nov 2018, 6:20 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
this is not a variable in fact. It's a constant. I should use const instead. e. g. const addition = (a, b) => a+b: Don't confuse it with JavaScript variables, in JavaScript, function can be declared as var or const too.
26th Nov 2018, 3:06 PM
CalviŐ˛
CalviŐ˛ - avatar
+ 2
[meta update] Found the presentation on Javascript WTF's; it does contain cursing so...your choice to watch or not. Kyle is writing some free-online books at http://YouDontKnowJS.com if you want to go that route instead. What the...JavaScript? [38:16] InfoQ, Kyle Simpson for Forward 2, "pulling out the crazy" from JavaScript https://www.youtube.com/watch?v=2pL28CcEijU&t=1151s "By the way that weird arrow syntax...that's ES6's new version of the function called an arrow function...but just to let you know arrow function is all so that people that make slides and write books can write less code. Really, that's its only purpose, so I put it on my slides so that my slides are shorter." It may be weary sarcasm :)
26th Nov 2018, 10:29 PM
Kirk Schafer
Kirk Schafer - avatar
0
Calvin, why would we define the function for a certain variable what difference does it make , than defining the function in isolation from variables , thanks
26th Nov 2018, 3:00 PM
Omar khaled
Omar khaled - avatar