In this example, I have shared return multiple values in JavaScript. Use the following JavaScript example:
1 2 3 | function getValues() { return [getFirstValue(), getSecondValue()]; } |
Then you can access them like so:
1 2 3 | var values = getValues(); var first = values[0]; var second = values[1]; |
If you want to put “labels” on each of the returned values (easier to maintain), you can return an object:
1 2 3 4 5 6 | function getValues() { return { first: getFirstValue(), second: getSecondValue(), }; } |
And to access them:
1 2 3 | var values = getValues(); var first = values.first; var second = values.second; |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.