Autolycus Autolycus - 6 months ago 17
Javascript Question

Return multiple values in JavaScript?

I am trying to return two values in JavaScript. Is that possible?

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

Answer

No, but you could return an array containing your values:

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

Then you can access them like so:

var codes = newCodes();
var dCodes = codes[0];
var dCodes2 = codes[1];

If you want to put "labels" on each of the returned values (easier to maintain), you can return an object:

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

And to access them:

var codes = newCodes();
var dCodes = codes.dCodes;
var dCodes2 = codes.dCodes2;