Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get items returned from an array in a function

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
}
like image 832
bladeedg Avatar asked Aug 11 '26 20:08

bladeedg


2 Answers

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

Update from

exampleFunction[0]

to

exampleFunction()[0] // paints "test"
like image 113
Nikhil Aggarwal Avatar answered Aug 14 '26 16:08

Nikhil Aggarwal


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 !

like image 24
PaulDennetiere Avatar answered Aug 14 '26 16:08

PaulDennetiere