Skip to content

Set

In one line

A Set is a collection of unique values — duplicates are automatically removed.

In simple words

A Set is a collection that only stores unique values. If you add a value that already exists, nothing happens — the set does not grow. Sets maintain insertion order and can hold any type of value.

Sets are useful for deduplication: new Set([1, 2, 2, 3]) creates a set with values 1, 2, 3. They are also efficient for membership testing — set.has(value) is typically faster than array.includes(value) for large collections.

Methods include add, has, delete, clear, and iteration via forEach or for...of. The size property gives the number of unique values. To convert a Set to an array, use [...set] or Array.from(set).

Example

javascript
const set = new Set([1, 2, 2, 3, 3, 3]);
console.log(set.size); // 3
set.add(4);
console.log(set.has(3)); // true
console.log([...set]); // [1, 2, 3, 4]

Sets automatically remove duplicates and provide efficient membership testing.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary