Javascript – Return multiple values in JavaScript


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

var newCodes = function() {  
    var dCodes =;
    var dCodes2 =;
    return dCodes, dCodes2;

Best Solution

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

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

Then you can access them like so:

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

With the latest ECMAScript 6 syntax*, you can also destructure the return value more intuitively:

const [first, second] = getValues();

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

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

And to access them:

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

Or with ES6 syntax:

const {first, second} = getValues();

* See this table for browser compatibility. Basically, all modern browsers aside from IE support this syntax, but you can compile ES6 code down to IE-compatible JavaScript at build time with tools like Babel.