Last Updated : 13 Aug, 2025
A Set in JavaScript is used to store a unique collection of items, meaning no duplicates are allowed.
// using an array
let s1 = new Set([10, 30, 30, 40, 40]);
console.log(s1);
let s2 = new Set(["gfg", "gfg", "geeks"]);
console.log(s2);
// using string
let s3 = new Set("fooooooood");
console.log(s3);
// an empty set
let s4 = new Set();
console.log(s4);
Set(3) { 10, 30, 40 } Set(2) { 'gfg', 'geeks' } Set(3) { 'f', 'o', 'd' } Set(0) {}How Set Internally Works in JavaScript
In JavaScript, hash tables are generally used to implement the property of unique values in the Set data structure. The core property of a Set is that it stores only unique values, and any duplicate values are automatically ignored.
Process of Storing Data in a Hash TableIn a hash table, each unique value is stored at a separate index. To retrieve a value from the hash table, the index must be known beforehand. This allows constant-time (O(1)) retrieval since the index is directly used to access the value.
Hash tables are considered optimized and fast for storing data, ensuring efficient management of the Set data structure's functionality, such as maintaining unique values and enabling quick lookups.
Key Characteristics of SetsJavaScript’s Set
object comes with several built-in methods to add, remove, check, and iterate over values. Below are the most commonly used methods with syntax and examples.
Adds the new element with a specified value at the end of the Set object.
Syntax :
mySet.add(value);
Example : In this example, we have added two numbers 23 and 12 into the set.
JavaScript
// Create a new set using Set() constructor
let myset = new Set();
// Append new elements to the
// set using add() method
myset.add(23);
myset.add(12);
// Print the modified set
console.log(myset);
Set.delete() :
deletes an element with the specified value from the Set object.
Syntax :
mySet.delete(value);
Example : Removing Elements from a Set with the delete() Method.
JavaScript
// Create a new set using Set() constructor
let myset = new Set();
// Append new elements to the set
// using add() method
myset.add(75);
myset.add(12);
// Print the modified set
console.log(myset);
// As 75 exists, it will be removed
// and it will return true
console.log(myset.delete(75));
console.log(myset);
Set(2) { 75, 12 } true Set(1) { 12 }
Set.clear() :
The Set clear() method in JavaScript is used for the removal of all the elements from a set and making it empty.
Syntax:
mySet.clear();
Example : Emptying set using Set clear() method.
JavaScript
// Create a new set using Set() constructor
let myset = new Set();
// Append new elements to the
// set using add() method
myset.add(23);
// Print the modified set
console.log(myset);
console.log(myset.size);
// The clear() method will remove
// all elements from the set
myset.clear();
// This will return 0 as there
// are no elements present in the Set
console.log(myset.size);
Set.entries() :
Returns an iterator object which contains an array having the entries of the set, in the insertion order.
Syntax :
mySet.entries()
Example :
JavaScript
let myset = new Set();
// Adding new elements to the set
myset.add("California");
myset.add("Seattle");
myset.add("Chicago");
// Creating an iterator object
const setIterator = myset.entries();
// Getting values with iterator
console.log(setIterator.next().value);
console.log(setIterator.next().value);
console.log(setIterator.next().value);
[ 'California', 'California' ] [ 'Seattle', 'Seattle' ] [ 'Chicago', 'Chicago' ]
Set.has() :
Returns true if the specified value is present in the Set object.
Syntax :
mySet.has(value);
Example : In this example, we have used sethas() method.
JavaScript
// Create a new set using Set() constructor
let myset = new Set();
// Append new elements to the
// set using add() method
myset.add(23);
myset.add(12);
// As 23 exists 23, it will return true
console.log(myset.has(23));
Set.values() :
Returns all the values from the Set in the same insertion order.
Syntax:
mySet.values()
Example :
JavaScript
let myset = new Set();
// Adding new element to the set
myset.add("California");
myset.add("Seattle");
myset.add("Chicago");
// Creating a iterator object
const setIterator = myset.values();
// Getting values with iterator
console.log(setIterator.next().value);
console.log(setIterator.next().value);
console.log(setIterator.next().value);
California Seattle Chicago
Set.keys() :
Also returns all the values from the Set in the insertion order.
Syntax :
keys()
Example : In this example, we will see the use of keys in the set.
JavaScript
let placeSet = new Set();
placeSet.add("America");
placeSet.add("England");
placeSet.add("Chicago");
const setIterator = placeSet.keys();
console.log(setIterator);
[Set Iterator] { 'America', 'England', 'Chicago' }
Set.forEach() :
executes the given function once for every element in the Set, in the insertion order.
Syntax :
forEach(function(value, key, set) {
/* ... */
}, thisArg)
Example : In this example, we will see the use of the forEach() method.
JavaScript
function setValue(value1, value2, mySet) {
console.log(`s[${value1}] = ${value2}`);
}
new Set(['Chicago', 'California', undefined])
.forEach(setValue);
s[Chicago] = Chicago s[California] = California s[undefined] = undefinedAlso Check
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4