Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda