1

I am trying to create a function that returns a two-element array value in Google Apps Script. Apparently I have misunderstood something because I thought that would be a simple matter of specifying:

      return [ value1 ][ value2 ] 

at the end of the function, but that is not working for me. So as a proof of concept, I wrote the following:

function testReturnArray() {
      var theValue = returnArray();
      Logger.log(theValue);
      }

function returnArray() { return ["a"]["b"]; }

When I run this code through the debugger, the log written is:

6:33:08 PM  Info    null

Clearly that's not my intended result. Can you please point me to the problem? I really would like to have two values returned from this function, and this is the easiest way I could think to do that. (Alternative being to return a class, but that may be overkill for my goal, and may also have a similar issue.)

3
  • 1
    what about [value1,value2] ? or [[value1],[value2]] ??? Commented Jan 11, 2021 at 23:52
  • 2
    function returnArray() { return ["a","b"]; } or function returnArray() { return [["a"],["b"]]; } depending on whether you want an array of elements or an array of arrays. Commented Jan 11, 2021 at 23:54
  • 1
    Yes, of course you're right. Silly me, syntax problem. I appreciate the help . Commented Jan 12, 2021 at 0:02

1 Answer 1

2

That's not how you write arrays in JavaScript. You need to use a comma to separate the values. There's a lot of content explaining arrays, but I'll suggest this as one to start with.

var theValue = returnArray();
console.log(theValue);

function returnArray() { return [ "a", "b" ]; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.