Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Return multiple values from function in an assigment

If I want to return a multiple values from a function, in an assigment, do I need to use an intermediate array to receive those values?

How am I used to do it in JavaScript:

  let f = () => return [1, 2, 3];
  let a, b, c, arr;
  arr = f();
  a = arr[0]; b = arr[1]; c = arr[2]

Is JavaScript like C, returning assignment to the first lhand operator, or is it possible to do something more flexible, like in this example from Ruby (without using loop):

  def f
    return 1, 2, 3
  end
  a, b, c = f

My motivation is simply lack of readability in the JavaScript method.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Issues

  1. return not needed in simple arrow function
  2. destructure assignment syntax

let f = () => [1, 2, 3];
let [a, b, c] = f();

console.log(a, b, c)

about 4 years ago · Juan Pablo Isaza Report

0

You can get values from an array either using index or using destructuring . If there are multiple values then you can easily destructure and store it in different variables in a single statement.

let f = () => [1, 2, 3];

//Using Destructuring
const [a, b, c] = f();
console.log(a, b, c);

// Accessing using index
const arr = f();
const m = arr[0];
const n = arr[1];
const o = arr[2];
console.log(m, n, o);

Since you are using an array to return multiple values then you can use either implicitly return in a function of you can explicitly return an array from it

1) Explicitly return

let f = () => {
  return [1, 2, 3];
};

const [a, b, c] = f();
console.log(a, b, c);

2) Implicitly return

let f = () => [1, 2, 3];

const [a, b, c] = f();
console.log(a, b, c);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!