To return multiple values from a JavaScript function, pack them into an array (best for ordered or positional values you unpack via array destructuring, such as [quotient, remainder]) or a plain object (best for named fields with clear semantics or optional defaults, such as { min, max }), because JavaScript functions only return a single value expression. Unpacking with destructuring assignment keeps callers readable while letting you rename properties, apply default values, or skip unneeded elements cleanly.
1. Returning an Array for Ordered or Positional Values
Return an array ([first, second]) when the values have a natural sequence or pair relationship and callers can unpack them positionally using array destructuring:
// Return ordered min and max values as a two-element array
function getMinMax(numbers) {
if (!Array.isArray(numbers) || numbers.length === 0) {
throw new RangeError("Expected a non-empty array of numbers.");
}
let min = numbers[0];
let max = numbers[0];
for (const value of numbers) {
if (value < min) min = value;
if (value > max) max = value;
}
return [min, max];
}
// Example usage with array destructuring:
const [lowest, highest] = getMinMax([42, 7, 19, 88, 3]);
const [, onlyHighest] = getMinMax([10, 25, 5]); // Skip first element with a comma
console.log({ lowest, highest, onlyHighest });
// { lowest: 3, highest: 88, onlyHighest: 25 }
- Easy local renaming: Callers can name destructured variables whatever fits the local scope (
[minVal, maxVal]or[start, end]). - Skip unused items: Commas inside
[ , second ]ignore leading values you do not need.
2. Returning an Object for Named Fields and Defaults
Return an object ({ quotient, remainder, ok }) when values have distinct meanings, when there are three or more return values, or when callers benefit from default values and order-independent destructuring:
// Return structured result fields as an object with safe defaults
function parsePort(rawValue, fallbackPort = 8080) {
const parsed = Number(rawValue);
const isValid = Number.isInteger(parsed) && parsed >= 1 && parsed <= 65535;
return {
port: isValid ? parsed : fallbackPort,
isValid,
source: isValid ? "input" : "fallback"
};
}
// Example usage with object destructuring and default fallback:
const { port, isValid, extra = "none" } = parsePort("3000");
console.log({ port, isValid, extra });
// { port: 3000, isValid: true, extra: "none" }
- Self-documenting call sites: Named keys like
isValidandsourceare clearer at a glance than positional array indices. - Order-independent unpacking: Destructuring
{ isValid, port }works regardless of property order in the returned object.
3. Choosing Between Arrays and Objects
Use an array for short positional tuples (such as [x, y], [error, data], or [state, setState]) and an object for rich records or functions whose return shape may grow over time without breaking positional callers:
// Compare tuple-style array return vs. named object return
function splitFullName(fullName) {
const parts = String(fullName).trim().split(/\s+/).filter(Boolean);
const firstName = parts[0] ?? "";
const lastName = parts.slice(1).join(" ");
return {
asTuple: [firstName, lastName],
asRecord: { firstName, lastName, hasLastName: Boolean(lastName) }
};
}
// Example usage:
const { asTuple, asRecord } = splitFullName("Zainab Tariq");
const [first, last] = asTuple;
console.log({ first, last, asRecord });
- Arrays break if positions shift: Inserting a new item at index
0shifts every subsequent destructured variable. - Objects scale cleanly: Adding a new property to a returned object does not disrupt existing destructuring patterns.