JavaScript Collections Guide
Tags: javascript, beginners, webdev, javascriptcollections
Published: 2023-06-26 19:39:39

In JavaScript collection is a collection, a group of data that can consist of various types of data that have different behaviors. The collection of the data can have various data types or this collection itself can be a specific data structure.
If you have not yet, after this post, I recommend you to read my post about [data types in JavaScript](https://dev.to/catherineisonline/javascript-data-types-5ec5) and then [data structures](https://dev.to/catherineisonline/javascript-for-beginners-data-structures-2j6i).

**There can be 3 types of collection in Javascript:**
- Indexed collections
- Keyed collections
- DOM collections

## Indexed collections
The indexed collection contains data that is ordered by index and can also be accessed using this index. An index means the position of data. For example, imagine people in a queue. The person most close to the counter has an index of 0, so they are at position 0. The next person has an index of 1, then the next one is 2, and so on. In JavaScript, the index always starts with 0.
Indexed collection can be an Array or TypedArray. Let's go through each of them.

## Array
The most popular and well-known collection of data is an array. An array is an index collection of ordered elements that have their own index and can be accessed by this index. Arrays can contain any data type and even other arrays. Regular arrays are not type-based. This means that you don't have to define what data type you are going to use inside the array. You can use everything together and anytime you want.

### Creating arrays
There are several ways to create an array. The easiest and most modern way is to create a variable with a value of empty square brackets. Later on or right away, you can place there any data you need.


![Creating arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iv429mr85nkc8caphx7m.png)



### Accessing array elements
To excess an array item, you can simply use an index (position) of the target item. Just don't forget that index starts with 0 so the first element is index 0, not 1.


![Accessing array elements](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzlycitwtuedbsg9zpo9.png)



### Destructing arrays
Destructing is a way of extracting data from an array in the most efficient way. Arrays can be very complex at times and even have several levels.
The most simple way to retrieve elements from an array and very easy for beginners to understand is targeting the elements by index. For example, if you need a specific item, you can count its index and retrieve this element.

![Destructing arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/te0diqa6hnvxjod70dlb.png)


Looks easy, right? But let's say I want to retrieve more.

![Destructing arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xm2v7npp6xhezi8gn6q.png)



But what if I have 100 elements? You are going to spend so much time retrieving everything one by one. By using a destructing we can minimize the code and time.

![Destructing arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqwmw5fljx6i7a0nsgi3.png)


It looks so much shorter and easier now. As you can see I placed new variable names in an array and each variable matches the value in an array by its position. My first variable is apple and the first element was assigned to an apple. There are many tricks and this is not the only thing you can do with destructing.
Image that you want to retrieve only the first value and you want to save the rest separately. It is absolutely possible!

![Destructing arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qnmt59web8bhl8akiy3o.png)



I repeated almost the same logic with a little difference. The first element is saved accordingly, just like before. However in order to save the rest of the elements I am using a rest parameter (the three dots) and a desired name of the variable.
What happens if we don't use the rest parameter? It will not save the rest of the elements from the array but save the one according to the position (index) and the rest will be simply ignored. Let's use the exact same example but without the rest parameter.

![Destructing arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/510tv91baiv58y3y62vg.png)



Another trick that you can use is skipping the specific index. For example, you want to retrieve the first and the third element. To do so, you can write an empty space a comma, and the next variable name.


![Destructing arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ys1dn9wh49uuea07zagc.png)



As you can see, we skipped an orange and moved on to the last element.
Arrays can often be flexible and change a lot. What happens if you add more variables than there are elements in an array? Nothing, it will simply be undefined.

![Destructing arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22p3ax5fasmy8xgodx7p.png)



However, it might not be convenient because you don't want the website users to see any values they don't understand. Until they are reading my post haha. To solve this issue you can also add a default values which will replace this undefined until there is an actual value.


![Destructing arrays](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vuocen04rf1pg0g5xfeu.png)



### Array methods
Arrays have various methods that can perform fast operations on the array without having to write everything from scratch. Methods are something similar to functions (not the same) that already exist in JavaScript so you don't have to create anything yourself. You can read more about the array methods here.


![Array methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3ja4p45pgos08cdoo3z.png)



## TypedArray
Typed arrays are not arrays. It's an array-like object, specifically a buffer. A buffer in JavaScript is a memory space (usually RAM) that stores binary data. This buffer, a typed array, stores integers in a specific order that is very similar to an array. Compared to arrays, typed arrays cannot be altered once created. Even though arrays and typed arrays are different, they do share several same methods but not all of them.
You might ask if they are so similar why does a typed array exist in the first place? As we already discussed, regular arrays can hold any data which we can manipulate at any time. However, the web keeps developing and there is more usage of heavy data like videos and audio. Such data is much harder to work with for the browser so it needs more time and effort! Of course, this is more complicated than this behind the scenes but all you need to know right now is that typed arrays store raw data that works much faster in the browser hence improving the performance which is vital for heavier and more complicated data.


![TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouerg1fok3s82h8dtco3.jpg)



### TypedArray architecture
The typed array is split into two parts - buffers and views. A buffer contains just data (memory) and nothing else while a view turns this data into a typed array by providing a data type of context (a data type that turns data into a typed array), a starting offset, and the number of elements.

### Creating TypedArray
In order to use a typed array you need to create an ArrayBuffer and then view it. When creating one you can either create a view first by indicating the desired size and type or you can create an array buffer and then the view that points to it.
The views can be various and a single array buffer can have different views.
Let's create a view for a typed array with a specific size and type.

![Creating TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf7tdkktphgazo30ipj7.png)



We created a view with the type Uint8Array and a size of five. We are going to cover the view types very soon so it's fine if you don't understand what it means right now. Let's see what it shows in the console.


![Creating TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1cbwm7yv815w9e9l4ao.png)


Pay attention to zeros. The five zeros you see is the size we have set previously. But they are empty right now and there is no data yet. Time to save some data there by using an index just like in an array.


![Creating TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwrm7u73aaywwqup1145.png)


Let's see the console again. Can you guess what will change?


![Creating TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3np2hfl8fh93v89lh10.png)


As you see, there is now the number 10 at the index 0 and 20 at the index 3, just as we set it.
Time to create an array buffer first and then the view, so it's a revered way of a typed array creation.


![Creating TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y22pu03ss85oy7xwpp5w.png)


Looks very similar, right? As you understand, the result is exactly the same.


![Creating TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx5ej8a3pzc15biz43ta.png)



### TypedArray views
Typed array views can be different as they can view different numeric types, for example, Int8, Uint32, Float64, and so on. It all depends on what you are working on and what type of data you need to process.


![TypedArray views](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7hpr503nwuxwek20m1n.jpg)


![TypedArray views](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bx1rk024szoqoncpidn.jpg)


![TypedArray views](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/06jt3fv0e9m6sufkkl92.jpg)


![TypedArray views](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oa2t6i16hui9xzmgjjjb.jpg)




### When to use TypedArray
Typed arrays are often used with web APIs. An API is a middleman between the application and the server. For example, when you are registering somewhere and then logging in, all the information is sent back and forth. By using a website you ask an API what you want from the web server.
Browsers have various built-in APIs that are using the typed arrays.

#### WebGL
WebGL(Web Graphics Library) was the first API where the typed arrays were used. It is an API that is rendering interactive 3D and 2D graphics without having the need to download or install anything. In our case of JavaScript, we can render it in HTML Canvas.

#### Canvas 2D
This API also works along with HTML Canvas and uses typed arrays. It provides various methods, objects, and properties to draw and manipulate the canvas element in real time.
XMLHttpRequest Level 2
The XMLHttpRequest2 API allows JavaScript to make HTTP requests in the browser. Before it, you had to parse a string into a typed array but now you can directly receive a typed array response.

#### FileReader
FileReader is a method of reading the content of a blob or a file. A blob is a file-like raw data read as binary data or text. And a file is a specific kind of blob. This is where typed arrays come into play as we have to work with binary data.

### Converting TypedArray
You can convert typed arrays from one type to another by using ArrayBuffer and you can also convert a regular array into a typed array.
To convert an array into a typed array you can use a built-in method typedArray.from() that accepts three parameters typedArray.from(source, map, arg). The source is a target array, the map is an options parameter for a function that is called on each element and the arg is the optional value used in the map function.


![Converting TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zti9wvn1uhfz9dkqnyo.png)



![Converting TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y2j74ykg0s7jnfb6tihs.png)


Converting from one typed array to another works just as simply. You simply target the desired typed array view and pass the typed array you want to convert.


![Converting TypedArray](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4wj9c2co8bwa48ipwga.png)



## Keyed collections
A keyed collection is a data collection ordered by keys instead of an index and consists of key-value pairs. In JavaScript, we have two types of keyed collections - Map and Set.

### Map
A map is a simple key-value pair object where you can iterate through elements in insertion order. Keys always need to be unique just like an index. To create a map you can use a map constructor and then add the desired elements by using a set method. The key name needs to be a string and a value can be any data type.


![Map](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r2lbq9tucbbdvw3wrpn9.png)


In order to access the key, you can use a get method and the key name. If you use a key that doesn't exist, it will return an undefined.


![Map](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tyswq14ktk8a0moffsi.png)



#### Map methods
Maps have various built-in objects including the set and get which are the most used ones.

##### Map.prototype.clear()
This method removes all existent key-value pairs and resets the map.


![Map methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5dd38elxcpidt7dtrkd.png)



#####  Map.prototype.delete()
The delete method returns true if the key existed and has been removed and false if it doesn't exist.


![Map methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/il30bbc47cnnj2l37d4u.png)



#####  Map.prototype.entries()
Returns a new iterator object with all key-value pairs. Iterator means that you can iterate over it, and loop over each key-value pair.


![Map methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pz8ro6bmkn60ak16sf67.png)



##### Map.prototype.keys()
This method is very similar to the previous one and also returns an iterator object that includes only keys.


![Map methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/at9ra28ov8t0xr5glzmh.png)



##### Map.prototype.values()
And the same works for this method but instead of keys it returns the values of the object.


![Map methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vzczrq3sdepjyus12e5.png)



##### Map.prototype.forEach()
This method works similarly to the forEach method in arrays. It executes a function for each key-value pair in the map in insertion order.


![Map methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ar8kzzbjv39ea5e4fnk.png)



##### Map.prototype.has()
The has method checks whether there is a value associated with the key that we are passing into this method. So it returns either true or false. Note that you need to pass the key, not the value.


![Map methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3e4z14ajgnmg6bihu2k.png)



#### Iterating over maps
To iterate over maps you can use a for…of loop. To target both key and value, a very easy way to do so is the usage of destructuring.


![Iterating over maps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8em6ek9mnxlfk0xh50h.png)


The same can be achieved using an entries method on the maps object. Compare them and find the difference.


![Iterating over maps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsg0dgvyb89cm9dglg6f.png)


If you want to work only with the keys, you can use another method for keys.


![Iterating over maps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79gs63yy0ycma4rzy4ja.png)


And the similar for working only with the values.


![Iterating over maps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfimqdkpdhlpnfslbf8y.png)



### WeakMap
A weak map in JavaScript is also a key-value pair collection. They look very similar however they are not exactly the same. Before we go further, let's create a simple weak map.


#### Difference between maps and weak maps
In weak maps, the keys can only be objects (or non-registered symbols) while values are more flexible.
However, what is the reason that it's called weak? The reason behind it is the fact that these key objects are a target of so-called garbage collection.

![WeakMap](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysenupnr086d5e23lrd8.png)

![WeakMap](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/br2ta6rgbrw212d45lhh.png)


#### Garbage collection
Garbage collection in JavaScript is automatic memory management. It monitors the memory allowance aka memory allocation and determines whether a specific block of memory is needed or not and gets rid of it if so. When the object keys are collected, their respective values are also collected unless they are strongly referred to something else, an object in this case.
In weak maps, you also don't have methods to access the keys as they are weak. Weak maps hold the reference to the key, not the key itself.
You cannot iterate over the weak map like you can with the map but they do share some similar methods like get, set, delete, and has.

#### Why do we need weak maps?
One of the main uses of weak maps is a memory leak. A memory leak in JavaScript is a process when JavaScript keeps allowing the specific block of memory and doesn't release it. The unused memory in the system keeps piling up, there is less memory and as a result, an application might crash.
Unused memory can take place in different situations. For example, unused event listeners that are attached to specific elements. An element might be removed but the unused event listener is still there. Now imagine a lot of event listeners like that that use a memory but we don't even need them. Other causes of memory leaks can be callbacks, timers (e.g. setTimeout), closures, and large data structures.
For instance, imagine that you want to track how many times a user clicked the ads on your website.
First, we will create a regular map where the user will be the key and the value will be how many times they clicked the ad. When the user leaves the website, we don't need the information about the clicks anymore.


![Why do we need weak maps?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvvsffgor6cqrljnbdqs.png)


Once the user object is gone, the object will remain in the map as a key so we manually would need to clear that map because it is not garbage collected and takes up the space in the memory. If you have a bad website maybe hardly anyone visits it but imagine if you had a lot of visitors. To test this, let's log the map to the console and see what it contains.


![Why do we need weak maps?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/de8en1oq6igo7wcyjz5e.png)


As you see, even though we said that the object of the user is null ("empty") the map still holds the information about the user.
This is where a weak map comes into play. A weak map is going to "weakly" refer to the user object which will allow the process of garbage collection. We are simply going to replace a map with a weak map and then see what happens to the information about the user once we try to clean it.


![Why do we need weak maps?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/niwbytz1sdw8gdhtzae9.png)



![Why do we need weak maps?](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zo4or5xuondvevnbk5xd.png)


As you can see, there is no more user object and a weak map got garbage collected! So awesome, right?!?!

### Set
Set is another keyed collection that consists of values. What makes this collection special is the fact that you can have only unique values and it's impossible to add any repetitive data. When you try to add a repetitive value, it will keep the one you added first.
Let's create a simple set to understand how it works.


![Set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m4g336mqyculh5k55j6a.png)


Next, I am going to try to add the repetitive value apple. What will happen?


![Set](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4gwp4jk12u0ludo95qyo.png)


Nothing. There is not going to be added anything. The repetitive value is not going to be added and the old one removed. It will be ignored. The apple result you see is the old value, not the new one. Because elements in the set are placed in the insertion order so if it was the new value it would be the last, not the first.

#### Set methods
Just like maps, sets also have various methods. Compared to maps we use add instead of set to add new values.

##### Set.prototype.clear()
This method removes all existent values and empties the set. But when you check an empty set it returns undefined. Instead, you can use a size property and it will show 0 when it's empty.


![Set methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qgpl2jx573xw3kwq0os.png)



##### Set.prototype.delete()
As the name suggests the delete method removes the value that we indicate.


![Set methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1dbjq75moxitl8buf7n.png)



##### Set.prototype.entries()
This method creates an iterable object of key-values where keys are equal to values in this case. So it becomes something similar to a map


![Set methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gph8fyl9rfbaoba0kt2t.png)



![Set methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljgfjj9ksgn25o8s4asv.png)



##### Set.prototype.keys()
This method returns an iterable object containing values. **Set.prototype.values()** is exactly the same.


![Set methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jjbdhvbpdtjblbiw9db9.png)



![Set methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqarv9lrarv54o7s1r4e.png)



##### Set.prototype.has()
Just like in a map, has returns true or false depending on whether it has a value we passed to the method.


![Set methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk2zu0bwlh1k8v3hq0nx.png)



##### Set.prototype.forEach()
This method executes a function for every single value in their insertion order.


![Set methods](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5p02hgassu92o0qc9kn.png)



#### Iterating over sets
You can iterate over sets using a for…of loop.


![Iterating over sets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5ishoafy7kwpb30sy9o.png)



### WeakSet
A weak set compared to a regular set is a collection of objects while sets can contain any data type. A weak set, just like a weak map holds information "loosely", for the purpose of temporary storage. Just like a set, a weak set also has unique elements and they are not repetitive. The weak sets just like weak maps are also used for garbage collection so make sure not to skip the topic about garbage collection that I have mentioned earlier.

![WeakSet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5ggort6kuefv8yf7fq2.png)



A weak set also supports methods like add, has, and delete however it cannot be iterated over and doesn't have a size property. A weak set usage is very good for the yes/no situation when let's say you are tracking your users and you check how many people are online right now. But when they log out you don't want to save the objects of the users. Just like with the weak maps, similar logic works with the weak set as it's garbage collected.

## DOM collections
If you already had a chance to work with HTML and Vanilla JavaScript together, you most likely have already worked with DOM collections. The skeleton of the website is built on HTML that consists of various tags. For a button there is a button tag, for a header there are various tags depending on the size, and so on. All these tags are objects. When you want some action to be done by clicking on the button, you target the button object with the help of JavaScript. You can target any HTML tag and you can do whatever you want with them. 
Sometimes, you might want to target all the buttons on the current page. The collection of these buttons becomes a DOM collection which is an array-like collection. It's more similar to an index collection though it looks like an array.
Let's check how many span tags there are on the main page of Amazon. To achieve that we can target span elements by using document.getElementsByTagName("span"). This will target all the spans.


![DOM collections](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc0dyg1g6tgwlcwivmk4.png)


Wow, so many spans!! And here we got all the spans in just one line.

## Conclusion
Congratulations! You reached the end of the JavaScript collections and this is a great start if you are a beginner. 
I hope you learned or re-learned something about indexed collections that are based on index values, keyed collections that consist of key-value pairs, and DOM collections that are simply groups of HTML tags. Of course, there is much more to learn about collections however if you are just getting started I believe this is somewhat enough for the start!