How to return multiple values from function in Node.js

4 Answers

0 votes
function f() { 
    const a = 9;
    const b = 4;
    const c = 398;
  
    return [a, b, c];
} 
  
const arr =  f();
  
const x = arr[0];
const y = arr[1];
const z = arr[2];
  
console.log(x, y, z); 
  
  
  
  
/*
run:
  
9 4 398
  
*/

 



answered Dec 31, 2021 by avibootz
0 votes
const [x, y, z] = (function(){ return [5, 98, 2]; })();
           
console.log(x, y, z); 
  
  
  
/*
run:
  
5 98 2
  
*/

 



answered Dec 31, 2021 by avibootz
0 votes
const {x, y, z} = (function(){ return {x: 95, y: 2, z: 8} })();
          
console.log(x, y, z); 
 
  
  
  
/*
run:
  
95 2 8
  
*/

 



answered Dec 31, 2021 by avibootz
0 votes
const f = function() {
    const a = 22;
    const b = 95;
    const c = 100;
    return {
        vala: a,
        valb: b,
        valc: c
    };
};
  
const rv = f();
  
const x = rv.vala;
const y = rv.valb;
const z = rv.valc;
           
console.log(x, y, z); 
  
 
  
  
  
/*
run:
  
22 95 100
  
*/

 



answered Dec 31, 2021 by avibootz

Related questions

...