Skip to main content
dublove
Legend
July 22, 2026
Answered

Why can't a function return an object or array, but it can return a value?

  • July 22, 2026
  • 2 replies
  • 29 views

I originally wanted to set the properties for creating a new cell style.
But it reminds me that the style is undefined?
Can it return sum but not newSty?

get('A', 'B');
alert(newSty);
//alert(sum);
//newSty.properties={

//}
function get(a, b) {
var baseSty = app.documents[0].cellStyles.itemByName(a);
sum = a + b;
var newSty = baseSty.parent.cellStyles.itemByName(b);
//return sum;
return newSty;
}

 

    Correct answer rob day

    alert(newSty) throws an error because newSty is only defined inside of the get(a, b) function.

     

    This does not throw an error if the document has cell styles named A and B:

     

    //x variable equals the returned value of get(a, b)
    var x = get('A', 'B')
    alert("Returned: " + x);
    alert("Style Name: " + x.name);

    function get(a, b) {
    var baseSty = app.documents[0].cellStyles.itemByName(a);
    //this doesn’t do anything
    sum = a + b;
    var newSty = baseSty.parent.cellStyles.itemByName(b);
    //returns cellStyle object;
    return newSty;
    }

     

     

    2 replies

    rob day
    Community Expert
    Community Expert
    July 22, 2026

    Are you trying to set the basedOn property of cell style B to cell style A?

    rob day
    Community Expert
    rob dayCommunity ExpertCorrect answer
    Community Expert
    July 22, 2026

    alert(newSty) throws an error because newSty is only defined inside of the get(a, b) function.

     

    This does not throw an error if the document has cell styles named A and B:

     

    //x variable equals the returned value of get(a, b)
    var x = get('A', 'B')
    alert("Returned: " + x);
    alert("Style Name: " + x.name);

    function get(a, b) {
    var baseSty = app.documents[0].cellStyles.itemByName(a);
    //this doesn’t do anything
    sum = a + b;
    var newSty = baseSty.parent.cellStyles.itemByName(b);
    //returns cellStyle object;
    return newSty;
    }

     

     

    dublove
    dubloveAuthor
    Legend
    July 23, 2026

    That's what it means.
    Thank you very much.
    When I had already fallen asleep, I realized that return must have a variable to take over the return value of the function.

    And if the sum can be displayed, it should be a global variable issue, not the function of retun.

    rob day
    Community Expert
    Community Expert
    July 23, 2026

    Don’t think you need the function—1 line produces the same result:

     

    var newSty = app.documents[0].cellStyles.itemByName("B");
    alert("Returned: " + newSty.name);