Return multiple values in JavaScript?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A JavaScript function still returns only one value, but that value can be a container holding several pieces of data. In practice, when developers say they want to return multiple values, they usually mean returning an array, an object, or a custom value type and then unpacking it at the call site.
Arrays Are the Smallest Option
If the returned values have a natural order and are closely related, an array is the simplest choice.
This works well when the meaning of each position is obvious and stable. It is common for coordinate pairs, min-max pairs, or functions that conceptually return a tuple.
The drawback is readability. If someone sees result[0] and result[1] later in the code, the meaning can become unclear unless the values are destructured immediately.
Objects Are Usually the Best General Answer
For most business logic, an object is the clearest return type because it names each value.
This is often the best pattern because:
- the caller does not need to remember positional ordering
- adding new fields later is less fragile
- named properties make the code self-documenting
Objects are especially good when the returned values are different kinds of data rather than one logical tuple.
Returning an Existing Domain Object
Sometimes the best answer is not "return multiple primitive values" but "return an object that represents the concept properly."
This is useful when the values belong together as one domain concept and you expect to attach behavior to them.
Destructuring Makes the Call Site Clean
Modern JavaScript is especially nice here because destructuring lets you unpack arrays or objects right where the function result is used.
Array destructuring:
Object destructuring:
That is usually cleaner than assigning the whole returned structure to a temporary variable and indexing into it later.
When Not to Return Many Values
Sometimes returning multiple values is a smell that the function is doing too much. If the caller only ever uses one of the values, or if the return shape keeps growing, consider whether the function should be split.
For example, this might be too broad:
That may be fine in a report builder, but in many codebases it means the function now has several responsibilities and should be decomposed.
Async Functions Follow the Same Rule
async functions do not change the pattern. They still return one value, but that one value is wrapped in a promise.
So the "multiple values" idea still works exactly the same way with promises.
Common Pitfalls
The most common mistake is choosing an array when the values do not have an obvious order. That makes the call site harder to read and easier to misuse.
Another issue is returning an object with too many unrelated properties, which can signal that the function has grown past one clear responsibility.
Developers also sometimes change the order of returned array values later, breaking callers silently. Objects are safer when the API may evolve.
Finally, do not forget that destructuring depends on the correct shape. If you change property names or positional order, update the call sites too.
Summary
- JavaScript functions return one value, but that value can hold multiple pieces of data.
- Arrays work well for ordered tuple-like results.
- Objects are usually the clearest general-purpose option.
- Returning a domain object is best when the values belong to one concept.
- Destructuring makes multi-value return patterns easy to consume cleanly.

