How to use set object in JavaScript

Set

The Set object contains a list of unique values. We can store both primitive and object inside a Set.

Create a set

let uniqueNum = new Set(); 

Add elements to a set

By using the add methods, we can add a new element to the set.

let uniqueNum = new Set();
console.log("Adding Values")
uniqueNum.add(1); // Set(1) {1}
uniqueNum.add(2); // Set(2) {1, 2}
console.log("Elements in set", uniqueNum); // Set(2) {1, 2}
console.log("\nAdding duplicate value '1' to set")
uniqueNum.add(1); // duplicate entry are not added.
console.log("Elements in set after adding duplicate value", uniqueNum); // Set(2) {1, 2}

Check if the set contains a value

By using the has method, we can check if there is a value present in the set.

let uniqueNum = new Set();
uniqueNum.add(1); // Set(1) {1}
uniqueNum.add(2); // Set(2) {1, 2}
console.log("Elements = ", uniqueNum); // Set(2) {1, 2}
console.log("\nChecking if 1 is present in set",uniqueNum.has(1)); // true
console.log("Checking if 5 is present in set",uniqueNum.has(5)); // false

Check the number of items present in the set

By using the size property, we can get the total number of items present in the set.

let uniqueNum = new Set();
uniqueNum.add(1); // Set(1) {1}
uniqueNum.add(2); // Set(2) {1, 2}
console.log("\Set = ", uniqueNum); // 2
console.log("Size of set", uniqueNum.size); // 2

Delete an item from the set

By using the delete method, we can delete an item from the set. The delete method returns true if the item is present in the set, otherwise it returns false.

let uniqueNum = new Set();
uniqueNum.add(1); // Set(1) {1}
uniqueNum.add(2); // Set(2) {1, 2}
console.log("deleting 1")
uniqueNum.delete(1); // true
console.log("After deleting set =", uniqueNum);
uniqueNum.delete(5); // false returned because 5 is not in set

Clear all elements of the set

By using the clear method, we can delete all the items in the set.

let uniqueNum = new Set();
uniqueNum.add(1); // Set(1) {1}
uniqueNum.add(2); // Set(2) {1, 2}
console.log("\nClearing the set");
uniqueNum.clear();
console.log("After clearing values in set is", uniqueNum);

Loop through items of the set

By using the forEach method, we can iterate through all the items of the set.

let fruits = new Set();
fruits.add('🍏')
fruits.add('🍎')
fruits.add('🍊')
fruits.forEach( fruit => console.log(fruit) );

Complete code can be downloaded below

let uniqueNum = new Set();
console.log("Adding Values")
uniqueNum.add(1); // Set(1) {1}
uniqueNum.add(2); // Set(2) {1, 2}
console.log("Elements in set", uniqueNum); // Set(2) {1, 2}
console.log("\nAdding duplicate value '1' to set")
uniqueNum.add(1); // duplicate entry are not added.
console.log("Elements in set after adding duplicate value", uniqueNum); // Set(2) {1, 2}
console.log("\nChecking if 1 is present in set",uniqueNum.has(1)); // true
console.log("Checking if 5 is present in set",uniqueNum.has(5)); // false
console.log("\nSize of set", uniqueNum.size); // 2
console.log("\ndeleting 1")
uniqueNum.delete(1); // true
console.log("After deleting set is", uniqueNum);
uniqueNum.delete(5); // false
console.log("\nClearing the set");
uniqueNum.clear();
console.log("After clearing values in set is", uniqueNum);
// using foreach
console.log("\n\nUsing for each to prrint elements in set")
let fruits = new Set();
fruits.add('🍏')
fruits.add('🍎')
fruits.add('🍊')
fruits.forEach( fruit => console.log(fruit) );

Additional information

We can create a new set with an array or string.

//using array
console.log("Creating set from Array");
let fruits = new Set( ['🍏', '🍎', '🍊'] );
console.log(fruits); // Set(3) {"🍏", "🍎", "🍊"}
// using string
console.log("\nCreating set from String");
fruits = new Set( '🍏🍎🍊🍇');
console.log(fruits); // Set(4) {"🍏", "🍎", "🍊", "🍇"}

We can add an array or object to a set.

let setTest = new Set([1,2]);
console.log(setTest); // Set(2) {1, 2}
console.log("\n Adding Array");
let arr = [1,2];
setTest.add(arr);
setTest.add([1,2]); //reference of arr is different from reference of [1,2]. So it is considered as new element.
setTest.add(arr); // this will be skipped
console.log(setTest); // Set(4) {1, 2, [1,2], [1,2]}
console.log("\n Adding Object");
setTest.add({ num : 10 });
console.log(setTest);

Free Resources