Arrow Functions with Block Bodies
You'll learn: How to write arrow functions that execute multiple statements by wrapping them in curly braces and using explicit return.
The Core Idea
So far you've seen arrow functions that return a single expression automatically. But what if your function needs to do multiple things—maybe log something, perform a calculation, then return a value? That's when you use curly braces {} to create a block body, just like regular functions.
Here's the key difference: when you add curly braces, JavaScript no longer assumes you're returning the expression. You must explicitly use the return keyword if you want to send a value back.
Single Expression (Implicit Return)
const double = x => x * 2;
console.log(double(5)); // 10
No braces needed—the result is automatically returned.
Block Body (Explicit Return)
const calculatePrice = price => {
const tax = price * 0.1;
const total = price + tax;
return total; // Must use return!
};
console.log(calculatePrice(100)); // 110
Notice the curly braces and the return statement. Without return, this function would give you undefined.
Analogy
Think of the single-expression arrow function as a vending machine: you press a button (call the function), and it instantly gives you the item (returns the value). A block-body arrow function is like a kitchen: you follow multiple steps (statements) and must deliberately hand over the finished dish (return it).
Key Takeaway: When an arrow function needs curly braces for multiple statements, you must explicitly return a value—implicit returns only work without braces.