Return multiple values from a function in JavaScript
This post will discuss how to return multiple values from a function in JavaScript.
1. Using an Array
We can return an array that contains the multiple values we want to return, and then access them by their index or use the destructuring assignment syntax to unpack them into variables. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
function getValues() { return [1, 2]; // return an array with two elements } let values = getValues(); // access the values by index let first = values[0]; let second = values[1]; // First: 1, Second: 2 console.log(`First: ${first}, Second: ${second}`); |
We can use a destructuring assignment to assign multiple values to variables directly from the function call, without explicitly accessing individual elements an array. This saves us from creating intermediate variables. For example, the following code uses the destructuring assignment to unpack values from arrays into corresponding variables.
|
1 2 3 4 5 6 7 8 9 |
function getValues() { return [1,2]; } // use destructuring assignment const [first, second] = getValues(); // assign `first` and `second` variables from the array // First: 1, Second: 2 console.log(`First: ${first}, Second: ${second}`); |
2. Using an Object
Another approach is to return an object that has properties for each value we want to return, and then access them by their property name or use the object destructuring syntax to unpack them into variables. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function getValues() { return { x: 1, y: 2 }; } let values = getValues(); // access the values by dot notation let first = values.x; let second = values.y; // First: 1, Second: 2 console.log(`First: ${first}, Second: ${second}`); // or use destructuring assignment to assign variables from the function call const {x, y} = getValues(); // x: 1, y: 2 console.log(`x: ${x}, y: ${y}`); |
3. Using a Generator
A third option is to return a generator that yields the multiple values we want to return, and then use the next() function or the for…of loop to iterate over the values. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function* getValues() { yield 1; // yield first value yield 2; // yield second value } // get the generator object let values = getValues(); // use next function to access the values let first = values.next().value; // get the first value let second = values.next().value; // get the second value // or use for…of loop to iterate over the values for (let name of getNames()) { // do something with name } // First: 1, Second: 2 console.log(`First: ${first}, Second: ${second}`); |
That’s all about returning multiple values from a function in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)