← Back to Chapters

JavaScript Set Methods

? JavaScript Set Methods

⚡ Quick Overview

A Set in JavaScript is a special collection that stores unique values of any type (primitives or objects). Unlike arrays, Sets automatically ignore duplicates and do not use index-based access.

In this topic you will learn how to create Sets with new Set() and work with the most common Set methods: add(), has(), forEach(), values(), keys() and entries().

? Key Concepts

  • Set stores only unique values (no duplicates allowed).
  • Values in a Set are not indexed like in arrays.
  • new Set(iterable) is used to create a Set (often from an array).
  • add(value) inserts a value; duplicates are silently ignored.
  • has(value) checks if a value exists in the Set (returns true / false).
  • forEach() loops over every value in the Set.
  • values() and keys() both return all values via an iterator.
  • entries() returns [value, value] pairs (for Map compatibility).

? Syntax and Theory

? Creating a Set with new Set()

The new Set() constructor creates a new Set object. You can pass an iterable (like an array) to pre-fill the Set.

? Create a Set with initial values
const fruits = new Set(["apple", "banana", "mango"]);
console.log(fruits);

➕ Adding Values with add()

The add() method inserts a new value into the Set. If the value is already present, the Set remains unchanged (no error is thrown).

? Add values to a Set
fruits.add("orange");
fruits.add("apple"); // "apple" already exists, so it will NOT be added again
console.log(fruits);

? Checking Existence with has()

The has() method returns true if a value exists in the Set, otherwise false.

? Check if a value is in the Set
console.log(fruits.has("banana")); // true
console.log(fruits.has("grape"));  // false

? Looping with forEach()

The forEach() method executes a callback function once for each value in the Set.

? Use forEach() to iterate over a Set
fruits.forEach(function(value) {
console.log(value);
});

? Getting Values with values() and keys()

The values() method returns an iterator containing all the values in the Set. In Sets, keys() is just an alias of values(), so both return the same iterator.

? Iterate using values() and keys()
const values = fruits.values();
for (const val of values) {
console.log(val);
}

const keys = fruits.keys();
for (const key of keys) {
console.log(key);
}

? Pairs with entries()

The entries() method returns an iterator of [value, value] pairs. This is mainly for compatibility with Map, where entries are [key, value] pairs.

? Use entries() to see [value, value] pairs
const entries = fruits.entries();
for (const [key, val] of entries) {
console.log(key, val);
}

? Output and Explanation

? What you will see in the console

  • After creating the Set with ["apple", "banana", "mango"], the Set contains exactly those three values.
  • When you call fruits.add("orange"), the Set now includes "orange".
  • Calling fruits.add("apple") does nothing extra, because "apple" is already in the Set (duplicates are ignored).
  • fruits.has("banana") prints true, while fruits.has("grape") prints false.
  • forEach(), values(), keys(), and entries() all let you visit each value in the Set, just in slightly different formats.

? Use Cases

  • Removing duplicates from an array (e.g., unique user IDs, unique tags).
  • Quick membership testing: “Is this item in the collection?” using has().
  • Tracking a set of visited items in algorithms (graphs, trees, etc.).
  • Storing unique values where order is not as important as uniqueness.

?️ Interactive Example

Type a fruit name and add it to the Set. Duplicates will not be added again.

Try adding the same fruit multiple times and watch the Set size.

? Current Set State

Values: [empty]

Size: 0

? Tips & Best Practices

  • Remember: Sets only store unique values. Duplicates are automatically ignored.
  • Use has() to check if a value exists before performing operations that depend on it.
  • In Sets, values() and keys() behave the same – both return all values.
  • entries() returns [value, value] pairs (not [key, value]), because Sets do not have separate keys.
  • If you need indexed access (like arr[0]), an Array is usually a better choice than a Set.

? Try It Yourself

  • Create a Set with duplicate values (for example from ["a", "b", "a", "c", "b"]) and log the Set to see how duplicates are handled.
  • Add a new value to the Set using add(), then verify its existence using has().
  • Iterate over the Set using:
    • forEach()
    • values() in a for...of loop
    • entries() in a for...of loop and log both items of the pair
  • Convert a Set back to an array using Array.from(mySet) or the spread operator [...mySet] and log the result.