Set in JavaScript
Set holds unique values with SameValueZero equality (NaN equals NaN here!) in insertion order: add/append, has tests, delete removes, size counts, clear empties. Unlike arrays, membership is effectively O(1) and duplicates structurally impossible.
Canonical uses: deduplication ([...new Set(arr)]), membership testing in hot paths, and set algebra via helpers (union/intersection as filter/has compositions). Values can be anything — but objects compare by reference, so two identical literals are two members.
Unique by construction
add() ignores present values — duplicates cannot exist, no manual checking.
O(1) membership
has() answers in constant time — the performance reason Sets replace includes() scans.
NaN equals NaN
SameValueZero unites NaN with itself — unlike ===, like includes.
Objects by reference
{a:1} !== {a:1} as members — distinct objects never deduplicate, however equal-looking.
let seen = new Set([1, 2, 2, 3]);console.log(seen.size, seen.has(2));console.log([...seen]);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.