Now, if you enjoy testing much of your code, especially in JS. You may find yourself using the console log to display information as you are running code. What’s even better, you can add multiple things to display, as an example in the REPL

This can be handy, in those instances when you’re checking multiple things inside of a function. But here’s something strange I did realize during a recent debug session with a friend. It works a bit different when returning values in a function to console log. Such as,

Even more interesting, it’s giving me the last variable in the return. Why thought?

Well the short answer is JavaScript doesn’t support a return multiple values. This is something that does appear in multiple languages. Some underlying reasons would include the mathematical nature of programming, or data structures you are working with. But surely, this is something that isn’t something we discovered. What if I needed a multiple value return?

So they trick to returning multiple values is using an array or an object with the return values. Then, making use of destructuring to unpack values from the array, or properties from objects.

To avoid any confusion, I’ve adjusted the variable names in my greeting function. So using an array, deconstructing would look as such

Likewise, using a returned object, we would our deconstructed variables as such

There you go. I did find it a bit odd overall. But should you find yourself needing multiple value returns in a function, be sure to desconstruct to make it easy peasy.

Till next time

Software Artificer, Random Pop Culture Generator