Arrow functions are a shorter way to write functions, using =>. You'll see them throughout modern JavaScript, especially as small "callback" functions passed to other functions, as in numbers.map(n => n * 2).
Arrow functions don't add any new abilities for most everyday tasks. Their value is brevity: a one-line arrow can replace three or four lines of a regular function, which keeps short helper code easy to read. This lesson shows how to shorten a function step by step, and where the two styles behave differently.
From a regular function to an arrow function
// regular function expression
const double = function (n) {
return n * 2;
};
// arrow function
const double = (n) => {
return n * 2;
};
// arrow with implicit return
const double = (n) => n * 2;
// one parameter: brackets optional
const double = n => n * 2;All four versions do exactly the same thing. Each step removes something that the shorter syntax makes unnecessary.
Arrow function shortcut rules
- Implicit return: if the body is a single expression with no
{ }, its value is returned automatically, without thereturnkeyword. - With
{ }, you needreturn, just as in a normal function. - No parameters: use empty brackets, as in
() => console.log("hi"). - One parameter: the brackets are optional, as in
x => x + 1. Many style guides keep them anyway for consistency. - Returning an object: wrap it in brackets so the braces aren't mistaken for a function body:
() => ({ ok: true }).
The most common arrow bug is adding curly braces and forgetting return. The function then returns undefined.
Arrow functions as callbacks
A callback is a function you pass to another function so it can be called later. Arrows make callbacks tidy:
setTimeout(() => console.log("1 second later"), 1000);
button.addEventListener("click", () => count++);
const evens = nums.filter(n => n % 2 === 0);Notice that you pass the function itself. Writing setTimeout(sayHi(), 1000) would call sayHi immediately and pass its result instead.
How arrow functions differ from regular functions
Arrow functions don't have their own this. They use the this of the code around them. That's usually helpful inside callbacks, but it means you shouldn't use arrows as object methods that rely on this (see Classes). Arrow functions also can't be used with new to create objects.
When not to use an arrow function
Arrow functions are short, but they aren't always the right choice:
- Object methods that use
thisshould be normal methods, because arrow functions don't get their ownthis. You'll see why in this and bind. - Constructors can't be arrow functions;
newdoesn't work with them. - Event handlers that rely on
thisbeing the element need a normal function, althoughevent.currentTargetworks in either. - Long, named helpers used in many places often read better as function declarations, which are also hoisted.
For short callbacks passed to map, filter, setTimeout and promise methods, arrow functions are the modern default.
JavaScript Arrow Functions Syntax
Arrow function forms
const f1 = () => "no params";
const f2 = x => x * 2;
const f3 = (a, b) => a + b;
const f4 = (a, b) => {
const sum = a + b;
return sum;
};
const f5 = () => ({ name: "object" });JavaScript Arrow Functions Summary
- Arrow functions are a shorter syntax for functions, written with
=>. - Without curly braces, an arrow function returns its expression automatically.
- With curly braces, you must write
return. - Wrap returned objects in brackets:
() => ({ ... }). - Arrows suit callbacks but don't have their own
this.
JavaScript Arrow Functions Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Same function, three ways
function areaClassic(w, h) {
return w * h;
}
const areaArrow = (w, h) => {
return w * h;
};
const areaShort = (w, h) => w * h;
console.log(areaClassic(3, 4), areaArrow(3, 4), areaShort(3, 4));Line-by-line explanation
- Lines 1 to 3A classic function declaration.
- Lines 5 to 7An arrow with a body still needs
return. - Line 9An arrow with an implicit return. Same result, one line.
Arrows as callbacks
const prices = [4, 18, 7, 25];
const withTax = prices.map(p => p * 1.2);
const cheap = prices.filter(p => p < 10);
const sorted = [...prices].sort((a, b) => b - a);
console.log(withTax);
console.log(cheap);
console.log(sorted);
setTimeout(() => console.log("⏰ This ran after 500ms"), 500);Line-by-line explanation
- Line 3
mapcalls the arrow for every price and builds a new array. - Line 4
filterkeeps prices where the arrow returns true. - Line 5
sortuses an arrow to compare two items (highest first). - Line 11
setTimeoutcalls the arrow later.
Returning objects
const makeUser = (name, age) => ({ name, age, isAdult: age >= 18 });
const broken = (name) => { name: name };
console.log(makeUser("Kai", 19));
console.log(broken("Kai"));Line-by-line explanation
- Line 1Brackets around
{ }mean "return this object".{ name, age }is shorthand for{ name: name, age: age }. - Line 3Without the brackets,
{ }is treated as a function body, so it returnsundefined.
Common JavaScript Arrow Functions Mistakes
Braces without return
const add = (a, b) => { a + b };
console.log(add(2, 3)); // undefinedconst add = (a, b) => a + b;
// or: const add = (a, b) => { return a + b; };Calling instead of passing
button.addEventListener("click", sayHi()); // runs immediately!button.addEventListener("click", sayHi);
button.addEventListener("click", () => sayHi("Sam"));Pass the function itself, or wrap the call in an arrow. Don't call it directly.
JavaScript Arrow Functions in Real Projects
Arrow functions in modern code
Open any modern codebase, whether React components, Node.js servers or browser extensions, and you'll see arrows everywhere: event handlers, array transformations, promise chains and small utility functions.
JavaScript Arrow Functions Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Turn this into a one-line arrow function with an implicit return.
const triple = n * 3;
console.log(triple(4)); // 12
(n) => n * 3 has no braces, so the value is returned automatically.What does this print?
const greet = name => `Hi ${name}`;
const nothing = () => {};
console.log(greet("Lu"), nothing());{} here is an empty function body, so it returns undefined.How do you return an object from an arrow function in one line?
isEven should return true/false and squareAll should return the squared array. Fix both arrows.
const isEven = (n) => { n % 2 === 0 };
const squareAll = (arr) => arr.map((x) => { x * x });
console.log(isEven(4), squareAll([1, 2, 3]));- isEven(4) is true
- isEven(7) is false
- squareAll([1, 2, 3]) is [1, 4, 9]
JavaScript Arrow Functions Quiz
3 questions to check what you learned.
Which is a valid arrow function?
const f = => 1;const f = () -> 1;const f = () => 1;const f = function => 1;
When does an arrow function return a value automatically?
- When the body is a single expression without curly braces
- Always
- When it has one parameter
- Never
Why shouldn't you use an arrow function as an object method that uses
this?- Arrows don't have their own
this; they use the surrounding one - Arrows are slower
- Arrows can't be stored in objects
- It's a syntax error
- Arrows don't have their own
JavaScript Arrow Functions Challenge
Arrow utilities
Write these as arrow functions with implicit returns:
const toCelsius = f => …that converts Fahrenheit:(f - 32) * 5 / 9const fullName = (first, last) => …returning"First Last"const makePoint = (x, y) => …returning the object{ x, y }
- toCelsius(212) is 100
- fullName("Ada", "Lovelace")
- makePoint(2, 5) returns { x: 2, y: 5 }
- Uses arrow functions