Can You Return Multiple Values in JavaScript?
Learn how to return multiple values in JavaScript using arrays, objects, and destructuring. This practical guide covers patterns, examples, and best practices for clean, readable code.

Yes. In JavaScript, a function can return multiple values by wrapping them in an array or an object, and then destructuring on the caller side. This pattern keeps functions focused while letting the caller pull the individual values as needed. You can choose arrays for positional returns or objects for named values.
Can you return multiple values in javascript?
According to JavaScripting, can you return multiple values in javascript? The short answer is yes. By default, JavaScript functions return a single value, but you can wrap multiple results in an array or an object and then destructure on the caller side. This approach keeps functions focused on a single responsibility while giving callers the values they need without extra boilerplate. In this guide, we’ll explore patterns, tradeoffs, and practical examples to make multi-value returns clear and maintainable.
function getPair() {
return [1, 2];
}
const [a, b] = getPair();
console.log(a, b); // 1 2You can also return an object for named values, which improves readability when the meaning of each value matters. For example:
function getConfig() {
return { host: 'localhost', port: 8080 };
}
const { host, port } = getConfig();
console.log(host, port); // localhost 8080Common variations include using rest parameters to capture extra values and using defaults in destructuring to guard against missing data.
shortSummary
Steps
Estimated time: 20-40 minutes
- 1
Define a function that returns multiple values with an array
Create a simple function that returns an array of values. This demonstrates positional data where the order matters. Name the items clearly so future readers understand what each slot represents.
Tip: Use descriptive element order and comments to explain what each position means. - 2
Consume the array with destructuring
Call the function and destructure the returned array into individual variables. This keeps the intent explicit and minimizes boilerplate.
Tip: Match the destructured variables to the array indices for readability. - 3
Switch to objects for named values
If values have names, return an object and destructure by property name. This makes the meaning of each value self-evident.
Tip: Prefer objects when values are not inherently ordered. - 4
Test and validate edge cases
Test with different lengths and missing values. Add defaults in destructuring to handle missing data gracefully.
Tip: Use default values to avoid undefined results.
Prerequisites
Required
- Required
- Required
- Basic knowledge of JavaScript functions, arrays, and objectsRequired
- Command line familiarityRequired
Commands
| Action | Command |
|---|---|
| Test inline multi-value returnRun in terminal to verify array return pattern | node -e 'const f=()=>[1,2,3]; console.log(f())' |
Questions & Answers
Can you return multiple values in JavaScript?
Yes. JavaScript functions can return multiple values by wrapping them in an array or an object, and then using destructuring at the caller. This pattern keeps functions small and expressive.
Yes, you can return multiple values by wrapping them in an array or object and then destructuring when you call the function.
Should I return an array or an object?
Use an array when the values are strictly ordered and consumed by position. Use an object when values have distinct meanings or when the data may grow with named fields, which makes code more maintainable.
Choose an array for positional data, an object for named data.
How do I access multiple values after returning them?
Destructure the returned value at the call site, e.g., const [a, b] = getPair(); or const { name, age } = getPerson();
Destructure the result to assign variables in a clean, single step.
Can a function return more than two values?
Yes. You can return any number of values by packing them in an array or an object and destructuring to retrieve as needed.
Yes, you can return any number of values using arrays or objects.
Are there pitfalls when returning multiple values?
Watch out for changing the return shape, which can break callers. Also avoid mutating returned objects and ensure the values’ meanings are clear.
Be careful about consistency and mutability when returning multiple values.
What to Remember
- Return multiple values by wrapping in an array or object
- Destructure the return value at the call site for clarity
- Use arrays for ordered data and objects for named values
- Provide defaults to guard against missing data
- Prefer readable patterns over clever but opaque implementations