Prev | Table of contents | Next
Set is similar to Map but doesn't have keys - values only. It is:
- Also iterable (has .entries(), .keys(), .clear(), .size etc. Constructor can accept an iterable.)
- Has "add" method instead of "set"
- Doesn't have "get" method - there's no reason to use set.get(value) instead of value itself
const set = new Set([
'value 1',
'value 2',
'value 3',
'value 4',
'value 4'
]);
console.log(set[Symbol.iterator] === set.entries); // false
console.log(set[Symbol.iterator] === set.values); // true
console.log(set.keys === set.values); // true
console.log([...set]); // ['value 1', 'value 2', 'value 3', 'value 4']
console.log([...set.entries()]); // ['value 1', 'value 2', 'value 3', 'value 4']
Usage example:
const divs = new Set([...document.querySelectorAll('div')]);
console.log(divs.size); // 42
divs.add(document.querySelector('div'));
console.log(divs.size); // 42 => no duplicates