The Set
object contains a list of unique values. We can store both primitive and object inside a Set
.
let uniqueNum = new 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}
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)); // trueconsole.log("Checking if 5 is present in set",uniqueNum.has(5)); // false
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); // 2console.log("Size of set", uniqueNum.size); // 2
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); // trueconsole.log("After deleting set =", uniqueNum);uniqueNum.delete(5); // false returned because 5 is not in 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);
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) );
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)); // trueconsole.log("Checking if 5 is present in set",uniqueNum.has(5)); // falseconsole.log("\nSize of set", uniqueNum.size); // 2console.log("\ndeleting 1")uniqueNum.delete(1); // trueconsole.log("After deleting set is", uniqueNum);uniqueNum.delete(5); // falseconsole.log("\nClearing the set");uniqueNum.clear();console.log("After clearing values in set is", uniqueNum);// using foreachconsole.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) );
We can create a new set with an array or string.
//using arrayconsole.log("Creating set from Array");let fruits = new Set( ['🍏', '🍎', '🍊'] );console.log(fruits); // Set(3) {"🍏", "🍎", "🍊"}// using stringconsole.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 skippedconsole.log(setTest); // Set(4) {1, 2, [1,2], [1,2]}console.log("\n Adding Object");setTest.add({ num : 10 });console.log(setTest);