Programming – Return multiple values in JavaScript?

JavaScript, like many other languages, doesn’t have a native way to return more than one value from a function call. Here is a little trick I use to get around that with object and JavaScript object deconstruction.

// return an object
function multipleReturns() {
  const a = 'one';
  const b = 'two';
  const c = 'three';
  return {a, b, c};
}
 
// use destruct on object returned
const {
  a,
  b,
  c
} = multipleReturns();
 
console.log(a, b, c);
// output 
// one two three

This is a short and sweet post, but I hope it’s helpful.

As always, leave a comment if you have any questions!

Leave a Reply

Your email address will not be published.