Coder Perfect

In JavaScript, how do I return multiple values?

Problem

In JavaScript, I’m attempting to return two values. Is that even possible?

var newCodes = function() {  
    var dCodes = fg.codecsCodes.rs;
    var dCodes2 = fg.codecsCodes2.rs;
    return dCodes, dCodes2;
};

Asked by Asim Zaidi

Solution #1

No, but you could return an array of your values instead:

function getValues() {
    return [getFirstValue(), getSecondValue()];
}

Then you can use the following method to get to them:

var values = getValues();
var first = values[0];
var second = values[1];

You can also destructure the return value more intuitively with the current ECMAScript 6 syntax*:

const [first, second] = getValues();

You can return an object if you want to put “labels” on each of the provided values (which is easier to manage).

function getValues() {
    return {
        first: getFirstValue(),
        second: getSecondValue(),
    };
}

And here’s how to get to them:

var values = getValues();
var first = values.first;
var second = values.second;

Alternatively, you can use ES6 syntax:

const {first, second} = getValues();

* For browser compatibility, see this table. Apart from Internet Explorer, all recent browsers support this syntax, however tools like Babel can translate ES6 code to IE-compatible JavaScript at build time.

Answered by Sasha Chedygov

Solution #2

You may do this with arrays and “destructuring assignments” starting with ECMAScript 6. These are not available in prior Javascript versions (i.e., neither the 3rd nor 5th editions of ECMAScript).

It allows you to assign to many variables at the same time:

var [x, y] = [1, 2];
x; // 1
y; // 2

// or

[x, y] = (function(){ return [3, 4]; })();
x; // 3
y; // 4

You can also use object destructuring in conjunction with property value shorthand to name and select the return values in an object:

let {baz, foo} = (function(){ return {foo: 3, bar: 500, baz: 40} })();
baz; // 40
foo; // 3

By the way, don’t be deceived by the notion that you may return 1, 2,…. with ECMAScript. What really happens there isn’t what it appears to be. A return statement expression — 1, 2, 3 — is nothing more than a comma operator applied sequentially to numeric literals (1, 2, and 3) that eventually evaluates to the value of its last expression — 3. As a result, return 1, 2, 3 and return 3 are functionally equivalent.

return 1, 2, 3;
// becomes
return 2, 3;
// becomes
return 3;

Answered by kangax

Solution #3

Simply return an actual object.

function newCodes(){
    var dCodes = fg.codecsCodes.rs; // Linked ICDs  
    var dCodes2 = fg.codecsCodes2.rs; //Linked CPTs       
    return {
        dCodes: dCodes, 
        dCodes2: dCodes2
    };  
}


var result = newCodes();
alert(result.dCodes);
alert(result.dCodes2);

Answered by Sean Kinsey

Solution #4

This is possible since ES6.

let newCodes = function() {  
    const dCodes = fg.codecsCodes.rs
    const dCodes2 = fg.codecsCodes2.rs
    return {dCodes, dCodes2}
};

let {dCodes, dCodes2} = newCodes()

dCodes, dCodes2 is a property value shorthand expression that is equivalent to dCodes: dCodes, dCodes2: dCodes2.

Object destructing assignment refers to the assignment on the last line. It extracts an object’s property value and assigns it to a variable with the same name. You could do it like this if you wanted to assign return values to variables with distinct names. newCodes = dCodes: x, dCodes2: y ()

Answered by Peracek

Solution #5

Ecmascript 6 provides “destructuring assignments” (as kangax mentioned), so you’ll be able to capture an array of values without having to create a named array or object for the sole purpose of capturing them in all browsers (not just Firefox).

//so to capture from this function
function myfunction()
{
 var n=0;var s=1;var w=2;var e=3;
 return [n,s,w,e];
}

//instead of having to make a named array or object like this
var IexistJusttoCapture = new Array();
IexistJusttoCapture = myfunction();
north=IexistJusttoCapture[0];
south=IexistJusttoCapture[1];
west=IexistJusttoCapture[2];
east=IexistJusttoCapture[3];

//you'll be able to just do this
[north, south, west, east] = myfunction(); 

You may already test it out in Firefox!

Answered by user3015682

Post is based on https://stackoverflow.com/questions/2917175/return-multiple-values-in-javascript