1. 6

In this article, I want to explore an interesting pattern that I am seeing more and more in JavaScript code which allows you to return multiple values from a function.

You probably know already that JavaScript does not support multiple return values natively, so this article will actually explore some ways to “simulate” this behaviour.


  2. 7

    Is returning a tuple from a function an emerging pattern?

    You probably know already that JavaScript does not support multiple return values natively

    …Yes it does? You demonstrated it.

    1. 5

      Edit: I was in a rush and forgot to provide a link, and now I can’t find the timestamp, I will later today: https://youtu.be/IOiZatlZtGU

      The point is a bit stretched but the idea is that some languages adhere to universal and very powerful mathematical ideas that are discovered, while some languages are piles of human, limited “inventions” on top of and because of other limited inventions.

      This is a prime example of what Philip Wadler calls an “invented” language. It’s confused on a totally basic level.

      Although I guess not having ergonomic pattern matching does that to you

      1. 2

        I’m not sure I follow this comment. Setting aside all of the hype about a “new pattern”, this is just a function returning an array. Wadler’s distinction between discovered and invented languages is about what syntax can or cannot be desugared to the lambda calculus. A function that returns a product type is absolutely expressed by the lambda calculus.

        1. 2

          I don’t know about Philip Wadler, can you provide some reference link? I’d be happy to read more about this to get your point.

          JavaScript is definitely a language that grow inorganically, it had to be adapted to ever changing needs, so it can’t be expected to be a particularly polished language. With that being said, i wouldn’t be so critic about it. The community has been doing an excellent work, especially in the last few years to make sure it can be a good general purpose language, filling the gaps and adding improvements where needed.

          Pattern marching is a good example of this, most people feel it’s something needed and there are already proposals that you can already play with today if you are willing to use transpilers.


            1. 1


        2. 4

          The JS way is returning a single object or array and some syntax sugar. Not a “”“real””” multiple return value.

          But it works.

          1. 3

            “natively” is the keyword here. Also python and rust seemengly support something like this, but in reality in those languages are leveraging tuples to return multiple values…

            1. 2

              Now I’m even more confused by the distinction you’re drawing. Tuples aren’t new or “emerging”. I understand the gist of your article is “JavaScript does not have tuples, but we can get by with other data structures.”

              What’s confusing me is: What is a “multiple” return value? Is a list a multiple return value? It potentially contains multiple values. Or is it just one list? If returning a list of length greater than two does not constitute “multiple” return values, then why is it a different case for a tuple? Why would a fake tuple in JavaScript constitute a “multiple” return value, whereas in other languages tuples are merely being “leveraged” to provide some other function?

              I don’t get it.

              1. 1

                Sorry if i am struggling here to provide more clarity. I still have to watch the video you mentioned, so i might still miss a bit of the context you are coming from.

                The point i wanted to make at the beginning of the article, even if not that important for the sake of the pattern, is that languages like go and lua have a concept of multiple return values that is baked in the language. Return values do not have to be wrapped in composite data structures. I am not sure how the computation stack is managed internally in those languages but i would expect, since this is a native feature, that multiple return values are very well optimized and that there’s no extra cost in allocating wrapper data structure. In JavaScript (as well as in python, rust, elixir and many other languages) this feature does not exist, so we have to simulate it somehow. The simulation comes with two main implications: 1. The pattern might not be very intuitive or obvious (unless coming from other languages); 2. There might be performance implications coming with this simulation…

                I hope this clarifies my point. I am happy to discuss further and thanks for taking the time to go through the article and for posting these comments

                1. 3

                  Here’s some context around where I’m coming from:

                  “Working Definition” of Function

                  A function is an equation for which any x that can be plugged into the equation will yield exactly one y out of the equation.

                  By that definition, a function can not have multiple return values. If your idea of “multiple return values” is just tuples, then I don’t think this is an “emerging” pattern or idea. This is something swathes of programmers have been doing in production daily for decades.

                  1. 2

                    I also had this reaction but I’m not familiar with Go or Lua, which are the given examples of languages that support this feature. At a glance, it looks like a syntax sugar over returning and destructuring a tuple. But I didn’t say anything because maybe these languages implement some behaviors around that syntax that remove equivalence to tuples. If that’s true then I feel the same grumpiness about invented languages expressed in this thread. But in any case, I agree that the post overstates what I assumed was a familiar and common use of arrays in JavaScript.

                    1. 1

                      Both fair points. Another thing that maybe makes sense to clarify is that by “emerging pattern” I specifically meant “emerging JavaScript pattern”.

                      Also, I don’t know Lua that deeply, but I can tell you that in go multiple return values are not implemented as tuples. The language itself supports the concept directly and there are a number of things you get out of the box (e.g. type checking).

          2. 3

            Aren’t these “destructors” and “spread” operators just syntactic sugar for a bunch of loops with heavy allocations and nested function calls in temporarily created iterator objects ? Does the VM optimize these away ? They cripple my already bad code reasoning :/

            1. 1

              Great question! Last section of the article is for you 😊

              1. 2


                I had read it. The article they point to is a great ref to this: https://docs.google.com/document/d/1hWb-lQW4NSG9yRpyyiAA_9Ktytd5lypLnVLhPX9vamE/edit

                Amazing that there is work being done here :) Maybe in the near future the VM will provide specialized versions of the iterators so they can produce that fine crafted bytecode asm.

                1. 1

                  Yeah, the guys in the V8 and chromium team are doing a fantastic work to make sure JS is as performant as it could possibly be in most of the use cases.

            2. 2

              I’d love to see this pattern used more where it makes sense. My biggest concern is making sure documentation is there for the return values, ideally editor supported.

              1. 1

                Totally agree. That’s a space where JS (and to be fair most of the dynamic languages) can still improve a lot.

              2. 2

                Good article! Thanks for sharing!

                1. 1

                  Thanks to you for taking the time to read it and provide some feedback :)