How to Return Two Values to a Function in ES6

Line drawing of a man smiling saying

Use Case: Check to see selected value matches one of two acceptable options out of a larger list. If so, continue. Then, if that selection matches a certain value, apply a condition.

I ran into this a while back and made a very cool (nerd-cool, that is) discovery via Google. A quick search led me to Dr. Axel Rauschmayer’s site, where I learned how to return multiple values to a function in ES6 (JavaScript).

Here’s an example of the use case above:

  1. If value is “Raptor” or “Viper,” then go down the path.
    1. If value is “Raptor,” do this special thing, then continue down the path.
    2. If value is not “Raptor” or “Viper,” exit

Here’s a working sample:

When funFunction is called, it checks the value of #vehicle-selection. If the value selected by the user is either “Raptor” or “Viper,” the isSpaceCraft function returns “true” AND the value as ready and value, respectively.

If ready is true, funFunction then returns value to the console. In place of the ternary, you can build out the subsequent process flows.

Special thanks to Dr. Axel and his article Multiple return values in ECMAScript 6.

Also published on paulmiller3000.com.

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
CONTACT US

Drop us a line

Schedule a complimentary consultation today.
0
Would love your thoughts, please comment.x
()
x