Set

Sets are like arrays.

The difference is they force unique values.

No typecasting in keys.

  1. var set = new Set();
  2. set.add(1);
  3. set.add(2);
  4. set.add(3);
  5. set.size; // logs 3. It is like Array.prototype.length
  6. set.has(2); // true
  7. set.clear(); // deletes all values
  8. set.delete(2); // deletes value 2

Another way to create a Set

  1. var set = new Set([1, 2, 3, 5]);

A new loop

  1. var set = new Set([1, 2, 3, 5]);
  2. for (let num of set) {
  3. console.log(num); // logs 1, 2, 3, 5
  4. }