Arrow Functions
Arrow functions are a different way to write functions - they are more compact, but slightly more limited.
Here are some key differences:
Arrow functions should not be used as
methodssince they do not have their own bindings tothis,argumentsorsuper.Arrow functions can't use the keyword
new.target.Arrow functions can't use
call,applyandbindmethods.Arrow functions can't be used as
constructors.Arrow functions can't use
yieldin it's body.
Let's have an example comparing the syntax of normal function declaration versus arrow functions:
info
When there exists multiple (or 0) arguments, one must wrap the arguments in parentheses for an arrow function.
An important syntax requirement of arrow functions is to also wrap the function body if there are more than 1 statements:
Overall the steps to creating an arrow function given a function are:
1) Remove the word
functionand place arrows between the arguments first body bracket2) Remove the body brackets (unless there's additional lines of processing) and remove the word
return3) Remove the argument's parantheses (unless theres multiple or none).
tip
Arrow functions are important to understand primarily because so many people use them!
More details regarding them are here: Arrow Functions - MDN