JavaScript cannot return multiple values directly from a single return statement, but there are several widely-used patterns to achieve the same result. The most common approaches are returning an array, an object, or using destructuring assignment to unpack values conveniently.
Returning an array works well when values are ordered: return [x, y, z]; can be unpacked with const [a, b, c] = myFunction();. This is clean and readable, especially when the order and meaning of each element is clear.
Returning an object is ideal when you want named values: return { name: 'John', age: 30 }; lets the caller extract specific values with const { name, age } = myFunction();. This is more self-documenting because each value has a label.
Modern JavaScript (ES6+) destructuring makes both patterns convenient and concise. You can also return a Map or Set for specific use cases, though these are less common.
Another pattern involves assigning values to object properties passed as parameters, or using output parameters, but this is considered less idiomatic in modern JavaScript.
For async functions, you still follow the same patterns—return an array or object containing the multiple values, and the caller uses await before destructuring.
Choosing between array and object depends on context: use arrays when values are positionally meaningful and consistent, use objects when you want named, self-documenting results that might be optional or sparse. Most modern code favors objects for clarity.