JavaScript: arrow functions
An arrow function is a concise function expression, useful for callbacks and small pieces of behavior.
What you will learn
- How to write parameters and a function body
- How implicit return works for one expression
- Why arrow functions do not create their own
this
Minimal examples
const add = (a, b) => a + b;
const doubled = [1, 2, 3].map(value => value * 2);
const greet = name => {
return `Hello, ${name}`;
};Without braces, the expression is returned automatically. With braces, write return when a result is needed.
Common mistakes
- Do not add braces and forget that braces create a block body with no implicit return.
- Arrow functions use the surrounding
this; choose a regular method when dynamic receiver binding is intended. - Use parentheses for multiple parameters and make the chosen style consistent.