Skip to content Skip to sidebar Skip to footer

Return Multiple Values And Access Them?

How would I structure this to return multiple values (message and name), and be able to access them in the js.html file?   --code.gs function createArtistTable(name) { var mes

Solution 1:

A function can only return one value.

So the way to do this is to wrap them together inside an array or object.

function return2Vals()
{
    var var1;
    var var2;
    //Code that does stuff with var1 and var2////////Create an array with the values and return it.var results = [var1, var2];
    return results;
}

Using the result:

var vals = return2Vals();
console.log("One of the return values is:", vals[0]);
console.log("The other return value is:", vals[1]);

Alternatively you could use an object and basically do whatever you want by using an object:

function returnSomeValsAsObj()
{
    var var1;
    var var2;
    //Code that does stuff with var1 and var2////////Create an object with the values and return it.var results = {primary_result: var1, secondary_result: var2, accompanying_message: "some message"};
    return results;
}

Using:

var results = returnSomeValsAsObj();
console.log(results.primary_result);
console.log(results.secondary_result);
console.log(results.accompanying_message);

Post a Comment for "Return Multiple Values And Access Them?"