Returning multiple values from a function using an array
Suppose the following getNames() function retrieves the first name and last name from a database in the backend or from the result of a third-party API call and returns them as elements of an array:
The following shows how to get the return value from the getNames() function:
In ES6, you can use the destructuring assignment syntax to unpack values from an array more intuitively, like this:
In this code, the firstName and lastName variables will take the first and second elements of the return array.