Sub topic 14
Returning Multiple Values
This chapter assumes you know array and object destructuring and have read the earlier chapters on return and on destructured parameters. A JavaScript function returns exactly one value. When you need several results, you bundle them into one container, usually an array or an object, and the caller unpacks it with destructuring.
Packing Results into One Value
The return statement accepts a single expression. Writing return a, b; does not return two values. The comma operator evaluates both and returns only b. To return more than one result, build a container:
function minMax(nums) {
return [Math.min(...nums), Math.max(...nums)];
}
function stats(nums) {
const total = nums.reduce((a, b) => a + b, 0);
return { total, mean: total / nums.length };
}
const [lo, hi] = minMax([3, 1, 4]); // lo = 1, hi = 4
const { mean } = stats([2, 4, 6]); // mean = 4
console.log(lo, hi, mean); // 1 4 4The function builds the container, and destructuring at the call site takes it apart into separate variables.
Arrays or Objects?
| Choice | Caller unpacks by | Good for | Risk |
|---|---|---|---|
| Array | Position | Two or three results with an obvious order | Swapping the order silently gives wrong values |
| Object | Property name | Several results, or results the caller may ignore | Typos in names give `undefined` |
Arrays let the caller pick any variable names, which is why React's useState returns [value, setValue]. Objects document themselves: { total, mean } tells the reader what each value is, and callers can take only the properties they need.
Common Errors
Relying on position for many values
const [a, , , d] = parse(text); is hard to read and breaks as soon as someone reorders the return. Once you have more than about three results, return an object.
Misspelling a property name
Object destructuring does not complain when a name is wrong:
function stats(nums) {
const total = nums.reduce((a, b) => a + b, 0);
return { total, mean: total / nums.length };
}
const { average } = stats([2, 4, 6]);
console.log(average); // undefined, the property is called "mean"Forgetting defaults for optional results
You can apply defaults while destructuring, using the same undefined-only rule that parameters follow:
function lookup(id) {
return id === 1 ? { name: "Ana", role: "admin" } : { name: "Unknown" };
}
const { name, role = "viewer" } = lookup(2);
console.log(name, role); // "Unknown" "viewer"Exercise
What does function pair() { return 1, 2; } const [a, b] = pair(); do, and how would you fix it?
Show answer
return 1, 2 uses the comma operator, so the function returns only 2. Destructuring a number as an array throws a TypeError because numbers are not iterable. To fix it, return an array: return [1, 2];, which makes a equal 1 and b equal 2.
Exercise
A function returns { count, errors, warnings }. A caller needs only errors, and wants it in a variable named errs. Write the destructuring.
Show answer
const { errors: errs } = check(); The other properties are ignored, which is one advantage of returning an object over an array.