AskHandle Blog
How Can I Return Multiple Values from a JavaScript Function?

How Can I Return Multiple Values from a JavaScript Function?
When writing JavaScript functions, you might need to send back more than one value to the code that called the function. JavaScript provides several ways to accomplish this task, and each method has its own benefits. Let's look at the most practical approaches to return multiple values from a function.
Using an Array
The simplest way to return multiple values is to pack them into an array. This method works well when you have values of the same type or when the order of values matters.
1function getUserInfo() {
2 const name = "Sam";
3 const age = 25;
4 const city = "Boston";
5
6 return [name, age, city];
7}
8
9const [userName, userAge, userCity] = getUserInfo();You can also receive the values using regular array indexing:
1const info = getUserInfo();
2const name = info;
3const age = info;
4const city = info;Using an Object
Objects offer a more structured way to return multiple values. This method is great when you want to give names to your returned values, making the code more readable and maintainable.
1function getProductDetails() {
2 return {
3 title: "Phone",
4 price: 599,
5 inStock: true
6 };
7}
8
9const {title, price, inStock} = getProductDetails();Using Class Instances
When you need to return complex data with methods attached to it, creating a class and returning its instance works well:
1class Rectangle {
2 constructor(width, height) {
3 this.width = width;
4 this.height = height;
5 }
6
7 getArea() {
8 return this.width * this.height;
9 }
10}
11
12function createRectangle(w, h) {
13 return new Rectangle(w, h);
14}
15
16const rect = createRectangle(5, 3);Multiple Return Statements
Sometimes you might want to return different sets of values based on conditions:
1function divideNumbers(a, b) {
2 if (b === 0) {
3 return {
4 success: false,
5 error: "Cannot divide by zero"
6 };
7 }
8
9 return {
10 success: true,
11 result: a / b
12 };
13}Tips for Choosing the Right Method
- Use arrays when dealing with a fixed number of values that have a specific order
- Pick objects when you need named values and want clearer code
- Select class instances when you need both data and behavior
- Choose multiple return statements for handling different scenarios
Common Mistakes to Avoid
Some developers make the mistake of trying to return multiple values directly:
1// This won't work
2function wrongWay() {
3 return value1, value2; // Only value2 will be returned
4}Instead, always use one of the methods mentioned above.
Performance Considerations
The choice of return method can affect performance. Arrays and objects create new memory allocations, while class instances might use more memory due to their prototype chain. For most applications, these differences are minimal, but in performance-critical code, you should measure and test different approaches.
Real-World Example
Here's a practical example of returning multiple values from a function that calculates various statistics:
1function analyzeNumbers(numbers) {
2 const sum = numbers.reduce((a, b) => a + b, 0);
3 const average = sum / numbers.length;
4 const max = Math.max(...numbers);
5 const min = Math.min(...numbers);
6
7 return {
8 sum,
9 average,
10 max,
11 min,
12 count: numbers.length
13 };
14}
15
16const stats = analyzeNumbers([1, 2, 3, 4, 5]);
17console.log(stats.average); // 3
18console.log(stats.max); // 5This approach makes the code clear and maintainable while providing all the necessary information in a single function call. The method you choose should match your specific needs and make your code easier to read and maintain.