r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

17

u/[deleted] Mar 19 '19

Is order guaranteed to be maintained with the Set data structure here?

26

u/Drugba Mar 19 '19 edited Mar 19 '19

No, it's not.

/u/BTOdell is correct. Insertion order is preserved when you spread a set.

26

u/BTOdell Mar 19 '19

The Set object in JavaScript preserves insertion order. So if you create a Set from an array, it will preserve the order when you spread it back out (but without duplicates).

9

u/Drugba Mar 19 '19

Oh, wow. You are totally correct.

I was under the impression that, because sets (the data structure) do not have an order, you wouldn't be able to guarantee order when you spread it, but that to totally wrong.