Returning values
A function returns a value with the return statement, handing a result back to the code that called it. Returning lets you compute something once and use it anywhere: store it in a variable, pass it to another function, or use it inside a larger expression. A function that never returns gives back undefined.
How return works
When JavaScript reaches a return, it stops running the function immediately and sends the value back. Any code after it in the same block does not run. The calling expression is then replaced by the returned value, so const total = addTax(100) stores whatever addTax handed back.
function addTax(amount, rate = 0.18) {
return amount + amount * rate;
}
const total = addTax(100); // 118
const doubled = addTax(100) * 2; // used inside an expression
// Early return keeps the main path unindented
function getDiscount(user) {
if (!user) return 0;
if (user.isPremium) return 0.2;
return 0.05;
}
// Returning several values: an object or an array
function stats(numbers) {
const sum = numbers.reduce((a, b) => a + b, 0);
return { sum, average: sum / numbers.length };
}
const { sum, average } = stats([4, 8, 12]); // 24, 8
// Arrow function with an implicit return
const isEven = (n) => n % 2 === 0;
Early returns and guard clauses
Returning as soon as you know the answer is a powerful way to simplify logic. In getDiscount, invalid input is handled first with a "guard clause", and special cases exit early. The main rule sits at the bottom without being buried in nested if...else blocks. Code written this way is easier to read and test.
Returning more than one value
A function can only return one value, but that value can be an object or array holding several results. Objects are clearer when the results have meanings, because the caller can destructure them by name. Arrays suit short, ordered pairs, a pattern you will later see in React's useState, which returns a value and a setter as a two-item array.
Return versus console.log
Beginners often confuse printing with returning. console.log only displays something in the developer console; the function still returns undefined, so the caller has nothing to work with. If a value needs to be used elsewhere, return it. Log it separately if you also want to see it.
Bugs that produce undefined
The trickiest one is a line break after return. JavaScript's automatic semicolon insertion turns return followed by a newline into return;, so the value on the next line is never returned. Keep the value on the same line, or open a bracket right after return.
Another common slip is returning from inside a callback and expecting the outer function to return. In items.forEach((item) => { if (item.id === id) return item; }), the return only exits the callback, and the outer function still returns undefined. Use items.find((item) => item.id === id) instead, and return its result.
Also check every path. If a function returns inside an if but not in the else case, some calls will quietly get undefined.
Pure functions
A function that depends only on its parameters and communicates only through its return value, without changing outside variables, is called pure. Pure functions always give the same output for the same input, which makes them predictable and easy to test.
Takeaway: return ends the function and hands back one value, use early returns for special cases, wrap multiple results in an object, and keep the returned value on the same line as the keyword.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.