Loading lessons...
Arrow Functions
Arrow Functions
Arrow functions are a shorter way to write functions, added in ES6.
The syntax
const add = (a, b) => {
return a + b;
};
Shorter forms
When there is only one expression, you can skip the braces and return:
const add = (a, b) => a + b;
With one parameter, you can skip the parentheses:
const double = x => x * 2;
Arrow vs regular
Regular function:
function add(a, b) {
return a + b;
}
Arrow function:
const add = (a, b) => a + b;
Same result, less typing.
Important differences
- Arrow functions do not have their own
this. - They cannot be used as constructors with
new. - They have no
argumentsobject.
TL;DR
- Arrow functions are a compact syntax:
(a, b) => a + b. - Single expressions skip braces and return.
- They have no own this, arguments, or new.