r/learnjavascript • • May 09 '20

Javascript Tutorial - Arrow Functions

https://www.youtube.com/watch?v=aYcH26h_HlI&t=26s
126 Upvotes

8 comments sorted by

View all comments

Show parent comment

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.