An arrow function is a shorter way to write a function expression, using => between the parameters and the body: const add = (a, b) => a + b;. Arrow functions arrived in ES2015 and are now the default choice for callbacks. They also differ from regular functions in one important way: they do not have their own this, arguments or new.target. This guide shows the syntax variations and when to pick a regular function instead.
If you can already write function declarations and expressions, arrow functions are only a new notation with a few extra rules.
An arrow function has three parts: a parameter list, the arrow =>, and a body. It is always an expression, so you assign it to a variable or pass it directly as an argument.
Use a block body with braces when you need several statements. The return keyword is required in that form, otherwise the function returns undefined.
When the body is a single expression, you can drop the braces and the return. The value of the expression is returned automatically.
The parameter rules are:
| Parameters | Syntax |
|---|---|
| None | () => value |
| One | n => value or (n) => value |
| Several | (a, b) => value |
| Default or destructured | (a = 1) => a, ({ id }) => id (parentheses required) |
| Rest | (...args) => args.length (parentheses required) |
Many teams always keep the parentheses, even for one parameter, because adding a second parameter later then needs no edit and the style is consistent.
Braces after the arrow are read as a function body, not an object. To return an object with an implicit return, wrap it in parentheses.
The most common use is short callbacks for array methods and promises. They remove noise and keep the logic close to the data.
Async arrow functions use async before the parameters: const load = async (url) => (await fetch(url)).json();.
A regular function gets its own this, decided by how it is called. An arrow function has no this of its own and uses the this of the surrounding code, fixed when the arrow is created. This is called lexical this.
In startRegular, this is undefined in strict mode code (class bodies are always strict), so the increment throws a TypeError. The arrow version works because it borrows this from startArrow.
Because of the missing this, arguments and prototype, arrow functions are the wrong tool in some places.
this. In { n: 1, inc: () => this.n++ }, this is not the object. Use method shorthand: { n: 1, inc() { this.n++; } }.new and throws a TypeError. Use a class or a regular function.this being the element. Use event.currentTarget inside an arrow, or a regular function.arguments. Use a rest parameter instead: (...args) => args.call, apply or bind must re-point. These cannot change an arrow function's this.Also note that an arrow function has no name unless you assign it to a variable. const add = (a, b) => a + b; gets the name add, which helps in stack traces, but an inline callback stays anonymous.
No meaningful difference exists in modern engines. Choose between them for behaviour (this) and readability, not speed.
No. An arrow function is an expression stored in a variable, so it follows the rules of const or let. Calling it before the line that defines it throws a ReferenceError.
yield inside an arrow function?No. Arrow functions cannot be generators. Use function* for that.
You probably used braces without return. Either add return or remove the braces for a single expression, and wrap object literals in parentheses.
Arrow functions give a compact syntax: parentheses for parameters, =>, then either an expression (implicit return) or a block body. They inherit this from the surrounding scope, which makes them ideal for callbacks, and they have no arguments, cannot be constructors, and are a poor fit for object methods. Use them for short functions and regular functions or method shorthand when you need their own this.