Skip to main content

JavaScript Set

A set is collection of unique elements. That can contain any type of values or objects reference. The specialty of set object is that are contains a collection of unique (duplicate) element. There are many ways to create a set in Javascript.

new Set([iterable])
//Create an empty set
let first = new Set();

//Create set, which is store integer values
let second = new Set([1,2,3,4]); 

//Create a set, which are store the reference of other sets
let third = new Set([first,second]);

//Create a set, Which is created by String value
let fourth = new Set("JS");

//Discarding the repeated element
let fifth = new Set("AAAABBBCCC"); //Display ABC


console.log(first);
console.log(second);
console.log(third);
console.log(fourth);
console.log(fifth);
Javascript Set Example
Set {}
Set { 1, 2, 3, 4 }
Set { Set {}, Set { 1, 2, 3, 4 } }
Set { 'J', 'S' }
Set { 'A', 'B', 'C' }

Method and attributes of Set

Method Overview
set.add(value) Add a element
set.delete(value) Remove given value element from Set
set.has(value) Returns true when value element are exist, otherwise false
set.size Return number of element
set.clear() Clears all element from the Set

set.add(value)

This method are adding a new element into of a given set.

//Create an empty set
let fruit = new Set();

//Add new set element
fruit.add("Apple");
fruit.add("Banana");
fruit.add("Orange");

console.log(fruit);
Add set element in javascript
Set { 'Apple', 'Banana', 'Orange' }

set.delete(value)

delete() method are remove given element from a set. When delete element are exist in set then this method are return true value otherwise returning false value.

//Create a set of 5 integer value
let element = new Set([1,2,3,4,5]); 
element.delete(4);
console.log(element);

element.delete(3);
console.log(element);

element.delete(5);
console.log(element);

//When element are not found
element.delete(7);
console.log(element);
Deleting set element in javascript
Set { 1, 2, 3, 5 }
Set { 1, 2, 5 }
Set { 1, 2 }
Set { 1, 2 }

set.has(value)

//define set
let element = new Set([10,20,30]); 
//Check that set element are exist or not
console.log(element.has(10));
console.log(element.has(20));
console.log(element.has(100));
console.log(element.has(true));
true
true
false
false

set.size

//define set
let set1 = new Set([10,20,30,40,100]); 
let set2 = new Set(["one","two"]); 
//get the total elements of set
console.log(set1.size);
console.log(set2.size);
5
2

Iteration over Set

//define set
let element = new Set([1,2,3,4,5]); 
//iterating set element
element.forEach(function(data){
	console.log(data);
}); 
1
2
3
4
5

entries() method of set are returning a iterator of set elements.

//define set
let element = new Set([1,2,3,4,5]); 
let record = element.entries(); 
console.log(record);

console.log(record.next());
console.log(record.next());
console.log(record.next());
console.log(record.next());
console.log(record.next());
console.log(record.next());
[Set Iterator] { 1, 2, 3, 4, 5 }
{ value: [ 1, 1 ], done: false }
{ value: [ 2, 2 ], done: false }
{ value: [ 3, 3 ], done: false }
{ value: [ 4, 4 ], done: false }
{ value: [ 5, 5 ], done: false }
{ value: undefined, done: true }




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment