Sets in Javascript

Get it, because you set a table…

Great things about Sets:

let one = new Set()
let two = new Set("Hello")
console.log(two)
//Set { "H", "e", "l", "o"})

.add()

one.add("Hello")
console.log(one)
//Set {"Hello"}

.has()

one.has("Hello")
//true

.size()

one.size()
//1

.forEach()

one.add("Hi")
one.add("Hello")
one.add("Ham Sandwich")
one.forEach(value => {
console.log(value)
})
//"Hi"
//"Hello"
//"Ham Sandwich"

.delete()

one.delete("Hi")// removes "Hi" from the set.

.clear()

one.clear()// clears out the entire set
// Set {}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store