3

I have a function which returns an array of two variables.

function exampleFunction() {
    var variable1 = 'test';
    var variable2 = 'test2';

    return [variable1, variable2];
}

Now in another function when I call exampleFunction how do I get the items from the array that is returned from it.

I have tried using:

if (exampleFunction[0] == true) {
    // do code here
}

2 Answers 2

8

To retrieve the values, you need to execute the function.

Update from

exampleFunction[0]

to

exampleFunction()[0] // paints "test"
Sign up to request clarification or add additional context in comments.

2 Comments

Ah thanks, just a little rookie mistake on my part! No wonder it kept returning undefined!
@bladeedg - Sometimes it happens :)
3

You can also get the returned array into an other variable, and then access from it :

var myArray = exampleFunction()
myArray[0]

Hope it answers your question !

1 Comment

Thanks, this also helped me see my ways!

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.