JS Sets
const setName = new Set([item1, item2]);
setName.add(item);
setName.has(item);
setName.delete(item);
Creating a Set
A Set is created with new Set(), optionally passing an iterable like an array, and any duplicate values in that initial array are automatically dropped, leaving only unique entries.
Example: Creating a Set
const nums = new Set([1, 2, 2, 3]);
console.log(nums); // duplicates dropped: {1, 2, 3}
add, delete, and has
add() inserts a new value into the Set, delete() removes a specific value and returns whether it was actually present, and has() checks whether a given value already exists in the Set.
Example: add, delete, and has
// Declare the constant `nums` as a new `Set` instance
// Declare the constant `nums` as a new `Set` instance
const nums = new Set();
// Call `nums.add(1)`
// Call `nums.add(1)`
nums.add(1);
// Call `nums.add(2)`
// Call `nums.add(2)`
nums.add(2);
// Print `nums.has(1)` to the console
// Print `nums.has(1)` to the console
console.log(nums.has(1));
// Call `nums.delete(1)`
// Call `nums.delete(1)`
nums.delete(1);
// Print `nums.has(1)` to the console
// Print `nums.has(1)` to the console
console.log(nums.has(1));
The size Property
A Set's size property reports how many unique values it currently contains, functioning the same conceptual role as length does for arrays and strings. Unlike array length, size is read-only and always reflects the Set's actual current contents.
Example: The size Property
const nums = new Set([1, 2, 3]);
console.log(nums.size);
Iterating a Set
Sets are iterable, letting you loop through every unique value using for...of, in the same order the values were originally added.
This insertion-order guarantee makes Sets predictable to iterate even though they're built around uniqueness rather than position.
Example: Iterating a Set
// Declare the constant `nums` as a new `Set` instance
// Declare the constant `nums` as a new `Set` instance
const nums = new Set([1, 2, 3]);
// Loop over `nums`, binding each item to `n`
// Loop over `nums`, binding each item to `n`
for (let n of nums) {
// Print `n` to the console
// Print `n` to the console
console.log(n);
}
Set vs Array
A Set automatically enforces uniqueness and offers very fast membership checks with has(), while an array allows duplicates and supports rich methods like map, filter, and indexed access that Sets don't have.
Example: Set vs Array
const set = new Set([1, 2, 2, 3]);
const arr = [1, 2, 2, 3];
console.log(set.size, arr.length); // 3 vs 4
console.log(set.has(2), arr.indexOf(2) !== -1);
- Trying to access a Set's items by index, like mySet[0], Sets have no index-based access, only iteration and has().
- Forgetting that a Set's size is checked with the size property, not length like an array.
- Assuming adding a duplicate value throws an error, it doesn't, add() on an existing value simply has no effect.
- A Set stores a collection of values where every value is guaranteed to be unique.
- add(), delete(), and has() are the primary methods for managing a Set's contents.
- Sets can be iterated with for...of and converted to arrays with the spread operator when array methods are needed.
Set has been supported in all major browsers since 2015 and is a fully standard part of modern JavaScript.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic