Sets

A Set is an object in JavaScript that is a collection of unique values.

A Set object can be created with the Set() constructor function.

const mySet = new Set();

Main methods

  • mySet.add(value): Adds a value to the set. If the value is already in the set, it doesn’t do anything.
  • mySet.delete(value): Removes the given value, returns true if the value existed in the set, otherwise false.
  • mySet.has(value): Returns true if the value exists in the set, otherwise false.
  • mySet.clear(): Deletes everything from the set.
  • mySet.size: Returns the number of elements in the set.

Adding value to a Set

mySet.add(1); // Set { 1 }
mySet.add('text'); // Set { 1, 'text' }
mySet.add(1); // Still, Set { 1, 'text' } because 1 is already in the set

Determining whether a value is in a set

console.log(mySet.has(1)); // Output: true
console.log(mySet.has('text')); // Output: true
console.log(mySet.has(5)); // Output: false

Retrieving the size of a set

console.log(mySet.size); // Output: 2

There are 2 values in mySet: 1 and text

Removing values from a set

console.log(mySet.delete(1)); // Output: true, Set { 'text' }
console.log(mySet.delete(5)); // Output: false, Set { 'text' }

The first statement returns true because mySet contain 1 as a value and has removed it. The second statement returns false because 5 is not a value in mySet.

Removing everything from a set

mySet.size; // 1, Set { 'text' }
mySet.clear(); // undefined, Set { size: 0 } in chrome

Initializing a set using iterables (e.g. arrays)

const newSet = new Set([2, true, 2, 'some string', { name: 'John' }]);
console.log(newSet); // Set { 2, true, 'some string', { name: 'John' } }
newSet.size; // 4

Codebyte Example

An interesting usage of Set is that we can use it to filter out duplicate values from an array.

Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn JavaScript on Codecademy