Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to return multiple values in JavaScript

Understanding the Problem

JavaScript, like many programming languages, generally allows a function to return only one value. You might wonder, "Well, what if I need to return more than one value?". Does this mean we're at a dead end? Absolutely not! In this blog, let's navigate through the winding roads of JavaScript to find the solution to this conundrum.

The Traditional Approach: Arrays

The most straightforward way to return multiple values from a function is to return them inside an array. An array, if you'd picture it, is like a train with multiple carriages. Each carriage can hold a different value, and they all move together as one entity.

Let's look at a simple function that calculates the area and perimeter of a rectangle:

function calculateRectangle(length, width) {
  var area = length * width;
  var perimeter = 2 * (length + width);
  return [area, perimeter];
}

Here, we're using an array (the square brackets) to return both area and perimeter. When calling the function, we can use array destructuring to access these values:

var [area, perimeter] = calculateRectangle(5, 3);
console.log(`Area: ${area}, Perimeter: ${perimeter}`);

The Named Approach: Objects

While arrays are great, they have a limitation. The order of the values matters. What if we mix up the order? The results could be disastrous, like serving salt instead of sugar in your coffee!

JavaScript objects offer a more flexible alternative. They're like a bag where you can put multiple things, each with a unique label. The order doesn't matter because we can access each item by its label.

Let's rewrite our previous function using an object:

function calculateRectangle(length, width) {
  var area = length * width;
  var perimeter = 2 * (length + width);
  return {area: area, perimeter: perimeter};
}

To access the values, we use object destructuring:

var {area, perimeter} = calculateRectangle(5, 3);
console.log(`Area: ${area}, Perimeter: ${perimeter}`);

A Versatile Approach: Tuples

If you're coming from a language like Python, you might be wondering about tuples. JavaScript doesn't natively support tuples, but we can mimic them using arrays. A tuple is like a special train that always has the same number of carriages in the same order.

Here's how we can use a "tuple" in JavaScript:

function calculateRectangle(length, width) {
  var area = length * width;
  var perimeter = 2 * (length + width);
  return [area, perimeter];
}

And to access the values:

var [area, perimeter] = calculateRectangle(5, 3);
console.log(`Area: ${area}, Perimeter: ${perimeter}`);

This might look exactly like the array example, and you're right. The difference lies not in the code, but in how we as programmers treat it. We should treat tuples as immutable and ordered collections of values.

Wrapping Up: Choosing the Right Tool

Now that we have seen three different ways to return multiple values in JavaScript, which one should we use? As with many things in programming, it depends.

  • If the order of the values matters and you want to keep things simple, use an array.
  • If you want to give names to your returned values for clarity, use an object.
  • If you're mimicking a tuple from another language and the order and number of values are always the same, use an array and treat it as a tuple.

In the end, the best tool is the one that makes your code clearer to the next person who reads it, which might be you in six months!

Let's close with a thought: Programming is like a journey. As you traverse through the landscapes of JavaScript, you're bound to encounter obstacles. However, with the right tools and a bit of creativity, no problem is insurmountable. Happy coding!