Member-only story
ES6 â Set vs Array â What and when?
What is Set and what is Array?
Everyone who works with JS until now is familiar with Array (donât tell me you donât). But what exactly is Array?
Well, in general, Array is type of structure representing block of data (numbers, objects, etcâ¦) allocated in consecutive memory.
Example: [1,2,3,2]
How about Set?
Set, more familiar as a Math concept, is an abstract data type which contains only distinct elements/objects without the need of being allocated orderly by index.
Example: {1,2,3}
Yup, by definition, Array and Set are technically different concepts.
One of the biggest differences here, you may notice, is that elements in Array can be duplicate (unless you tell it not to be), and in Set, they just canât (regardless what you decide).
In addition, Array is considered as âindexed collectionâ type of data structure, while Set is considered as âkeyed collectionâ.
A quick reminder for those who donât remember,
Indexed collections are collections of data which are ordered by an index value
Keyed collections are collections which use keys; these contain elements which are iterable in theâ¦