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().
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).The new Set() constructor creates a new Set object. You can pass an iterable (like an array) to pre-fill the Set.
const fruits = new Set(["apple", "banana", "mango"]);
console.log(fruits);
The add() method inserts a new value into the Set. If the value is already present, the Set remains unchanged (no error is thrown).
fruits.add("orange");
fruits.add("apple"); // "apple" already exists, so it will NOT be added again
console.log(fruits);
The has() method returns true if a value exists in the Set, otherwise false.
console.log(fruits.has("banana")); // true
console.log(fruits.has("grape")); // false
The forEach() method executes a callback function once for each value in the Set.
fruits.forEach(function(value) {
console.log(value);
});
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.
const values = fruits.values();
for (const val of values) {
console.log(val);
}
const keys = fruits.keys();
for (const key of keys) {
console.log(key);
}
The entries() method returns an iterator of [value, value] pairs. This is mainly for compatibility with Map, where entries are [key, value] pairs.
const entries = fruits.entries();
for (const [key, val] of entries) {
console.log(key, val);
}
["apple", "banana", "mango"], the Set contains exactly those three values.fruits.add("orange"), the Set now includes "orange".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.has().Type a fruit name and add it to the Set. Duplicates will not be added again.
Values: [empty]
Size: 0
has() to check if a value exists before performing operations that depend on it.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.arr[0]), an Array is usually a better choice than a Set.["a", "b", "a", "c", "b"]) and log the Set to see how duplicates are handled.add(), then verify its existence using has().forEach()values() in a for...of loopentries() in a for...of loop and log both items of the pairArray.from(mySet) or the spread operator [...mySet] and log the result.