Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

same function return multiple values in terms of objects, array or function in javascript via design pattern? [duplicate]

How can i return multiple values from the function in javascript. And how to use that.??

like image 794
Kunvar Singh Avatar asked Apr 21 '26 23:04

Kunvar Singh


2 Answers

You can't do that.

But, you can return an array or an object which contains your values.

function doSomething(a,b){
    return [a,b];
  //return {a,b};
}
console.log(doSomething(1,2));

If you want to return many values you can use destructing operator in order to find out all the values.

function doSomething(a,b,c,d,e,f){
      return {a,b,c,d,e,f};
}
let {a,b,c,d,e,f}=doSomething(1,2,3,4,5,6);
console.log(a,b,c,d,e,f);
like image 168
Mihai Alexandru-Ionut Avatar answered Apr 24 '26 13:04

Mihai Alexandru-Ionut


Functions by definition can only return one value. However, you can pack your values in an array or an object:

function greeting(){
    return {
        name: "Andy",
        message: "Hello world"
    };
}
like image 44
Derek 朕會功夫 Avatar answered Apr 24 '26 12:04

Derek 朕會功夫



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!