Arrow Functions
Arrow functions are a shorter way to write functions, introduced in ES2015. They are perfect for small callbacks, such as those you pass to array methods. They also handle the keyword this differently from regular functions, which is useful in some places and a trap in others.
Arrow syntax
An arrow function drops the function keyword and puts an arrow => between the parameters and the body. Here is the same function written three ways:
// 1. function declaration
function add(a, b) {
return a + b;
}
// 2. arrow function with a block body
const addArrow = (a, b) => {
return a + b;
};
// 3. arrow function with a concise body
const addShort = (a, b) => a + b;
When the body is a single expression, you can drop the braces and the return: the value of the expression is returned automatically. This is called an implicit return.
The short-cut rules
- One parameter: the brackets are optional:
n => n * n. (Many style guides keep them anyway:(n) => n * n.) - No parameters: empty brackets are required:
() => "Habari!". - Several statements: use braces, and then you must write
returnyourself. - Returning an object literal: wrap it in brackets, otherwise the braces are read as a block:
(name) => ({ name, active: true }).
const square = n => n * n;
const add = (a, b) => a + b;
const greet = () => "Habari!";
const withVat = (price) => {
const tax = price * 0.16;
return price + tax;
};
console.log(square(9));
console.log(add(4, 6));
console.log(greet());
console.log(withVat(100));
Once you add braces, the implicit return is gone. const double = n => { n * 2 }; returns undefined, because the braces make a block with no return statement. Either remove the braces or write { return n * 2; }.
Arrows as callbacks
Arrow functions shine when a function is passed to another function. Compare:
const prices = [100, 250, 400];
const doubledOld = prices.map(function (p) {
return p * 2;
});
const doubled = prices.map(p => p * 2);
console.log(doubled);
const makeUser = (name) => ({ name, active: true });
console.log(makeUser("Imani"));
Both versions of the map call do the same thing; the arrow version simply reads like a formula: "each p becomes p * 2". You'll use this style constantly on the Array Methods page.
How this behaves
Inside a regular function, the keyword this depends on how the function is called. Call it as a method, obj.method(), and this is obj. Call the same function on its own, method(), and this is undefined in strict mode (or the global window object in old, non-strict scripts).
An arrow function has no this of its own. It uses the this of the code surrounding it, at the place where it was written. This makes arrows ideal for callbacks inside methods:
const team = {
name: "Kuza FC",
players: ["Amani", "Baraka"],
introduce() {
this.players.forEach((player) => {
console.log(player + " plays for " + this.name);
});
}
};
team.introduce();
introduce is a normal method, so when called as team.introduce() its this is team. The arrow callback borrows that same this, so this.name is "Kuza FC". If you replaced the arrow with function (player) { ... }, that inner function would be called by forEach as a plain function and get its own this (undefined in strict mode), so this.name would no longer find the team.
Don't use an arrow function as an object method that needs this. In const user = { name: "Zawadi", hi: () => "Hi, " + this.name }; the arrow takes this from the surrounding code (the top of the script), not from user, so user.hi() does not give "Hi, Zawadi". Use method shorthand instead: hi() { return "Hi, " + this.name; }. For the same reason, in an event listener use a regular function if you rely on this being the clicked element.
Other differences
| Regular function | Arrow function | |
|---|---|---|
Own this | Yes, set by how it is called | No, uses the surrounding this |
arguments object | Yes | No (use ...rest instead) |
Can be used with new | Yes | No |
| Hoisted (declaration form) | Yes | No, it is always an expression |
| Best for | Methods, constructors, top-level named functions | Short callbacks, especially inside methods |
Key points
- Arrow syntax:
(params) => expressionor(params) => { statements }. - A concise body returns its value automatically; a block body needs
return. - Wrap a returned object literal in brackets:
() => ({ ... }). - Arrows don't have their own
this; they use thethisof where they are written. - Use arrows for callbacks, and regular methods for object functions that use
this.
Tip: use the ← and → keys to move between pages.
