Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Set uniqueness

A Set is a collection of values without keys where each value appears at most once. It is useful for membership checks and removing duplicates.

How it works

A Set stores values without keys and enforces uniqueness: adding a value already present has no effect. It can be initialized from an iterable, queried with has(), and converted back to an array when needed; iteration follows insertion order.

Syntax

new Set(iterable)

Parameters

NameTypeDescription
iterableiterableAn optional iterable whose values are copied into the new Set.

Returns

A Set object containing unique values from the optional iterable.

Key points

  • Set is designed for collections of unique values and does not associate each value with a separate key.
  • Pass an iterable to the constructor to populate a Set immediately; an array is a common source.
  • Use has(value) for a boolean membership test without changing the Set.
  • clear() empties the entire collection in one operation.
  • The size property gives the number of values currently held by the Set.

Common mistakes

  • Calling add() again with an existing value leaves the Set unchanged, although add() still returns the Set itself.

Example: Remove duplicate values with Set

const values = [1, 2, 2, 3, 1];
const uniqueValues = new Set(values);

console.log([...uniqueValues]); // [1, 2, 3]
console.log(uniqueValues.has(2)); // true

Constructing a Set from an iterable keeps each value only once, so spreading it back into an array removes duplicates.

When to use it

Deduplicate arrays

Remove repeated primitive values from incoming arrays or user-provided lists.

Fast membership tracking

Track whether identifiers, permissions, or tags have already been seen.

Enforce uniqueness

Maintain a collection that must not contain repeated entries.

Interview questions

QWhat is the defining property of a Set?

A Set is a collection of values rather than key-value pairs, and each value can occur only once. It is useful when membership and uniqueness matter more than indexed access.

Follow-up: How can an array be deduplicated? Construct a Set from the array and spread or convert that Set back into an array.

QHow does new Set(iterable) initialize a Set?

The constructor accepts an iterable, commonly an array, and adds its elements to the new Set. Duplicate elements are therefore retained only once.

Follow-up: What happens if no iterable is supplied? The constructor creates an empty Set.

QIn what order are Set values iterated?

A Set can be traversed with for...of or forEach(), and its values are visited in insertion order. This preserves the order in which distinct values were successfully added.

Follow-up: Which operation checks membership without modifying the collection? Use set.has(value), which returns a boolean.

  • Set
  • new Set(iterable)
  • Set.prototype.add()
  • Set.prototype.has()
  • Set.prototype.delete()
  • Set.prototype.clear()
  • Set.prototype.size()
  • for...of

Source: Map and Set - The Modern JavaScript Tutorial. Checked 2026-10-15.