Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Set — a collection of unique values

Set stores each distinct value only once and supports iteration in insertion order. Use it to remove duplicates and test membership without manually searching an array.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

new Set()
new Set(iterable)
set.add(value)
set.has(value)
set.delete(value)
set.clear()
set.size

Set is a built-in collection of values, not key–value pairs. Create one with new Set() or new Set(iterable), for example from an array. The constructor requires new. Duplicates do not create additional elements.

add(value) inserts a value and returns the same set, allowing chained calls. has(value) reports membership, while delete(value) returns true if an element was removed or false if it was absent. clear() removes all elements. Read the element count through the size property, not length or size().

Uniqueness follows SameValueZero: NaN is equivalent to NaN, and +0 and -0 represent the same element. 1 and "1" remain different. Objects are compared by identity, not contents: two separately created {} objects are two elements.

Iteration with for...of or [...set] preserves insertion order. Adding an existing value again does not change its position; deleting and re-adding it moves it to the end. Set does not provide numeric-index access to its elements.

This entry covers the core API, excluding newer set-operation methods. The language specification is not a browser compatibility table: check availability in the target environment; Node 24 does not prove browser support.

Examples

const values = new Set([3, 1, 3, 2]);
values.add(1);
console.log(values.size);
console.log(values.has(2));
console.log(values.delete(1));
values.add(1);
console.log([...values].join(','));

Result

3
true
true
3,2,1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources