---
# System prepended metadata

title: Set & Map in Javascript

---

# Set & Map in Javascript

 ```
 let set = new Set();  // initializes a new set
 ```
 
 ### properties on set
  ```
  1. set.add(value)
  2. set.clear() // removes all
  3. set.delete(val) // deletes val and returns boolean
  4. set.has(val) // check if exists and return boolean
  5. set.size() // gives size
  ```
 
 ### iterate over set with for..of
 ```
 for(let item of set) console.log(item);
 ```
 ### set to array and vice-versa
 ```
 let set  = new Set([1,2,3,4,4]);
 let arr = [...set];
 ```
 
 
 ```
 let map = new Map();  // initializes a new map
 ```
 
 ### properties on map
  ```
  1. set.set(key,value)
  2. set.clear() // removes all
  3. set.delete(key) // deletes key val and returns boolean
  4. set.has(key) // check if exists and return boolean
  5. set.size() // gives size
  ```
 
 ### iterate over map with for..of
 ```
 1. for(let key of map.keys()) console.log(key);
 
 2.for(let [key, value] of map) console.log(key,'=',value);
 ```
 
 
 ### array to map conversion
 ```
 let map  = new map([['key1','val1'],['key2','val2'],['key3','val3']]);
 ```