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

JavaScript · Class

Set

Set stores unique values of any type, including primitives and object references. It preserves successful insertion order and provides methods for adding, removing, testing, and iterating values.

How it works

A Set is a collection that keeps each value at most once, including primitives and object references. Construct it with an optional iterable, add values with add(), test membership with has(), and traverse values in the order successful additions occurred.

Syntax

new Set()
new Set(iterable)

Parameters

NameTypeDescription
iterableIterableAn optional iterable whose elements are added to the new Set; omitted, null, or undefined produces an empty Set.

Returns

A new Set object.

Key points

  • Set can contain primitives and object references, while preventing duplicate entries.
  • Adding an already-present value does not create another entry.
  • Iteration exposes values in the order in which successful add() calls first inserted them.
  • Set is a value collection rather than a key-value structure like Map.
  • Use add(value) to insert conditionally; the method preserves uniqueness.
  • Use has(value) for a boolean membership test.
  • entries() yields two-item arrays whose key and value are both the Set value.

Common mistakes

  • Membership uses SameValueZero: NaN matches NaN, and 0 and -0 are treated as equal.
  • Objects are compared by reference, so two separately created objects with identical contents are different Set values.
  • Calling Set without new throws a TypeError; use new Set() or new Set(iterable).

Example: Unique values with Set

const tags = new Set(["js", "web", "js"]);
tags.add("language");

console.log(tags.size); // 3
console.log(tags.has("web")); // true
console.log([...tags]); // ["js", "web", "language"]

Creates a Set from an iterable, removes the duplicate string, adds another value, tests membership, and converts the insertion-ordered values to an array.

When to use it

Deduplicate data

Remove repeated primitive values from an array while preserving first-seen order.

Fast membership tracking

Check whether an item has already been encountered during processing.

Visited-object tracking

Track object references that have been visited without duplicating the same reference.

Interview questions

QWhat problem does Set solve, and what order does it preserve?

Set stores unique values of any type, including primitives and object references. A value can occur only once, and iteration follows successful insertion order.

Follow-up: How are object values compared? They use object identity, so only the same object reference is recognized as the existing value.

QHow does Set determine whether two values are the same?

Set equality uses SameValueZero. Consequently, NaN is considered equal to NaN, while object values are compared by reference.

Follow-up: What happens with duplicate objects? Distinct object literals remain distinct entries even when their properties are equal.

QHow is a Set initialized from existing data?

The constructor accepts an optional iterable, such as an array, and adds its elements to the new Set. If the argument is omitted, null, or undefined, the Set starts empty.

Follow-up: How can a Set be converted to an array? Spread the Set into an array, which consumes its insertion-order iterator.

  • Set()
  • Set.prototype.add()
  • Set.prototype.clear()
  • Set.prototype.delete()
  • Set.prototype.entries()
  • Set.prototype.forEach()
  • Set.prototype.has()
  • Set.prototype.values()

Source: Set - JavaScript | MDN Web Docs. Checked 2026-10-15.