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]}

Last updated

Was this helpful?