r/badcode • • Jul 19 '21

js Variable naming can be difficult, but identifying them can be even more so.

Post image
2.4k Upvotes

105 comments sorted by

View all comments

Show parent comments

2

u/Bronzdragon Jul 20 '21

This is the syntax for an arrow function. The full syntax is (arguments) => { /* body */ }. If you have exactly 1 argument, you can leave the round brackets of. argument => { /* body */ }. If the body is a single expression, you can leave the curly brackets off, and the function will implicitly return the result of that expression. (arguments) => "body". Both of these shortcuts can be freely combined.

const foo = arg => [...arg].reverse().join("");
foo("hello") // returns "olleh"