← Back
destructuring javascript

Return more than one value

Some programming languages implements the tuple data structure. It's (normally) a fixed length, immutable list, that has very good performance.

Tuples are used lot of times to return more than one value from a function.

It's a common (an idiomatic) pattern in Javascript to use Array destructuring for the same purpose:

function calculate(/* ... */) {
// some calculations
// return the values wrapped into an array
return [total, average];
}

function myBussinesLogic(/* ... */) {
// destructure the array to obtain the values
const [total, average] = calculate(/* ... */);
}

Happy destructuring!