DEV Community

M.Ark
M.Ark

Posted on

Arrow functions in JavaScript

Arrow functions in JavaScript Instead of the function keyword, it uses an arrow (=>) made up of an equal sign and a greater-than character (not to be confused with the greater than-or-equal operator, which is written >=).

Image description
The arrow comes after the list of parameters and is followed by the function’s body.
It expresses something like “this input (the parameters) produces this result (the body)”.
When there is only one parameter name, you can omit the parentheses around the parameter list. If the body is a single expression, rather than a block in braces, that expression will be returned from the function. So, these two definitions of square do the same thing:

Image description

When an arrow function has no parameters at all, its parameter list is just an empty set of parentheses.

Image description
There’s no deep reason to have both arrow functions and function expressions in the language.
Arrow functions were added in 2015, mostly to make it possible to write small function expressions in a less verbose way

Top comments (0)