JavaScript Set Objects - Episode II - Gettin' Jiggy Wit' it
Tags: javascript, setobject, jiggywitit
Published: 2019-10-02 03:56:36

---
title: JavaScript Set Objects - Episode II - Gettin' Jiggy Wit' it
published: true
description: Journey with me as I explore the capabilities of JS Set Objects. The second post in a JS Set Series!
tags: #JS, #SetObject, #JiggyWitIt
---

In my [first blog](https://dev.to/anders1216/js-set-part-1-5e72) of this series I introduced the Set object. I outlined an instance in which the use of a Set object and its uniqueness check simplified an already overly complex function by making a placeholder hash and two iterative methods unnecessary. In this blog I will to cover some more methods built into the JS Set Objects and where they are useful. 


###`Set.prototype.add(value)` && `Set.prototype.delete(value)`
Like many other data stores Set objects have basic functions do introduce and remove elements from them. They are identical in use to the similar functions for Arrays and Hashes. 

```javascript
let set1 = new Set("set is so cool!")

  #=> Set(9) {"s","e","t"," ","i","o","c","l","!"}

set1.add("Wat?")

  #=> Set(10) {"s","e","t"," ","i","o","c","l","!","Wat?"}

set1.delete("i")

  #=> Set(9) {"s","e","t"," ","o","c","l","!","Wat?"}

```

###`Set.prototype.has(value)`
Similar to Arrays Set objects have a built in class method that checks for particular values within the Set object. The method returns a boolean value depending on if the value is present(true) or not(false). This is super useful for powerful conditional functions.
```javascript
if (set1.has("i")) {
  set1.delete("Wat?")
  set1.add("WAT!?")
}

console.log(set1)

  #=> Set(9) {"s","e","t"," ","o","c","l","!","Wat?"}
```

###`Set.prototype.clear()`
As described in my first blog of this series Set objects are akin to a Hash Array hybrid in a way. Unlike either Set objects have an included function that works to empty the elements of the Set. 

```javascript
if (set1.has("Wat?")) {
  set1.clear()
}

  #=> Set(0)
```

###`Set.prototype.entries()`

The Set object's built in class methods also break the Array-Hash paradigm with the `Set.prototype.entries()`method. This class method returns a `SetIterator` object that contains all the elements of the original Set object as key-value pairs where each key and value are the element (maybe succinct but definitely confusing examples FTW). 

```javascript
set2 = new Set("set is so cool!")

  #=> Set(9) {"s","e","t"," ","o","c","l","!"}

entries = set2.entries()

console.log(entries)

  #=> SetIterator {"s" => "s", "e" => "e", "t" => "t", " " => " ", "i" => 
  "i", "o" => "o", "c" => "c", "l" => "l", "!" => "!"}

```

###`Set.prototype.values()` &&/|| `Set.prototype.keys()`

Back into the Hash-Array relationship Set objects have a similar function to hashes that returns the values or keys of the Set. Unlike Hashes the `Set.prototype.values()` and `Set.prototype.keys()` both return the same  SetIterator object containing all of the elements of the original Set even when key-value pairs are present. Whence I am a famous programmer I will return to this post and explain why a SetIterator that is identical to the set its a part of. My inkling is that it has to do with efficiency. SetIterator objects have one class method, SetIterator.prototype.next(). ~*_My assumption_*~ is that they take less memory to store because they are similar to linked lists. Anyway, examples:

```javascript
set2.values()

  #=> SetIterator {"s","e","t"," ","o","c","l","!"}

set2.keys()

  #=> SetIterator {"s","e","t"," ","o","c","l","!"}

let set3 = new Set([{"key": "value"},{"keys": "values"}])

set3.keys()

  #=> SetIterator {{"key": "value"},{"keys": "values"}}

set3.values()

  #=> SetIterator {{"key": "value"},{"keys": "values"}}
```
###Next Time!
That is about it for Set. Thank you for checking out my blog! I plan to revisit this blog in the coming months when I am more knowledgeable about Set and all its wonderful uses. Till next time!