MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/ggfz1d/javascript_tutorial_arrow_functions/fq4wlbi/?context=3
r/learnjavascript • u/ddmanton7220 • May 09 '20
8 comments sorted by
View all comments
9
For some reason I initially struggled to grasp arrow functions. Something about .filter(function(item) { was easier to understand for me at the time.
.filter(function(item) {
As with anything repetition is key. Write it enough, and you'll get it.
4 u/drumstix42 May 10 '20 edited May 10 '20 For learning the syntax, you can take it in steps to go from standard sytanx to arrow function sytanx.Standard, multiline function[0,1,2,3,4,5,6].filter(function(item) { return item > 3; })Remove the function portion before the parenthesis (non-valid code):[0,1,2,3,4,5,6].filter((item) { return item > 3; })Add the arrow portion => (valid code again):[0,1,2,3,4,5,6].filter((item) => { return item > 3; })Optionally single-line it: Remove the braces { and } and the return. The return is implied in a single line without the braces.[0,1,2,3,4,5,6].filter((item) => item > 3)Also note, when there is only 1 parameter, the parenthesis are optional. E.g.:[0,1,2,3,4,5,6].filter(item => item > 3)But if there are no parameters, or more than 1, the parenthesis are required.
4
For learning the syntax, you can take it in steps to go from standard sytanx to arrow function sytanx.
Standard, multiline function
[0,1,2,3,4,5,6].filter(function(item) { return item > 3; })
Remove the function portion before the parenthesis (non-valid code):
function
[0,1,2,3,4,5,6].filter((item) { return item > 3; })
Add the arrow portion => (valid code again):
=>
[0,1,2,3,4,5,6].filter((item) => { return item > 3; })
Optionally single-line it: Remove the braces { and } and the return. The return is implied in a single line without the braces.
{
}
return
[0,1,2,3,4,5,6].filter((item) => item > 3)
Also note, when there is only 1 parameter, the parenthesis are optional. E.g.:
[0,1,2,3,4,5,6].filter(item => item > 3)
But if there are no parameters, or more than 1, the parenthesis are required.
9
u/[deleted] May 09 '20
For some reason I initially struggled to grasp arrow functions. Something about
.filter(function(item) {was easier to understand for me at the time.As with anything repetition is key. Write it enough, and you'll get it.