ES6 in Depth
  • Introduction
  • Installation
  • Block Scope
    • Constant
    • Use cases
  • Template String
    • New String Methods
    • Tagged Template Literal
  • New Number Methods
  • Arrow Function
    • Default Parameters
    • Lexical This Scope
    • Where not to use arrow function
  • Object Enhancement
  • New Array Methods
    • For Of
  • Spread Operator
  • Destructuring
    • Array Destructuring
  • Class
  • Symbols
  • New Data Structures
    • Set
    • Map
    • WeakSet
    • WeakMap
    • Iterators
    • Generators
  • Promise
  • Import Export
Powered by GitBook
On this page

Was this helpful?

  1. New Data Structures

Set

A data structure of storing unique values, it's not an array

var s = new Set([1,2,2,2,2,3]);

s // [1,2,3]
s.add(4); // [1,2,3,4] , it's chainable - s.add(4).add(3).add(5)
s.delete(2); // [1,3,4]
s.clear(); // return undefined ( clears set )
s.has(3); // true
s.forEach(function(item){
    console.log(item); // 1,3,4
})

// also use for of loop to iterate over set as it returns iterator

--------------------------------------
var s = new Set([1,2,2,2,2,3]);

console.log(s.keys());
console.log(s.values());
console.log(s.entries());

// output
main.js:3 SetIterator {1, 2, 3}
main.js:4 SetIterator {1, 2, 3}
main.js:5 SetIterator {[1, 1], [2, 2], [3, 3]}
PreviousNew Data StructuresNextMap

Last updated 4 years ago

Was this helpful?