Loading Please Wait...
A JavaScript Set is a collection of unique values. Each value can only occur once in a Set.
The JS Set has the following methods and properties:
The JS new Set() method is used to create a JS Set. You can pass variable, values, array, objects etc.
let num = 1;
const alphabets = ['x', 'y', 'z'];
const set1 = new Set();
const set2 = new Set('a');
const set3 = new Set(num);
const set3 = new Set(['a', 'b', 'c']);
const set4 = new Set(alphabets);
The JS add() method is used to add values to the Set. If you add equal elements, only the first will be saved.
const mySet = new Set();
mySet.add('a');
mySet.add('b');
mySet.add('c');
mySet.add('d');
mySet.add('a');
mySet.add('b');
mySet.add('a');
The JS forEach() method invokes (calls) a function for each Set element.
const mySet = new Set(["a","b","c"]);
// List all Elements
let text = "";
mySet.forEach (function(value) {
text += value;
});
The JS values() method returns a new iterator object containing all the values in a Set so you can use the Iterator object to access the elements.
const mySet = new Set(["a","b","c"]);
mySet.values() // JS Set Iterator having Set values
let text = "";
for (const x of mySet.values()) {
text += x;
}
The JS has() method returns true if the Set has the passed element.
const mySet = new Set(["a","b","c"]);
mySet.has('a'); // true
mySet.has('h'); // false
The JS delete() method deletes the passed element from the Set.
const mySet = new Set(["a","b","c"]);
mySet.delete('a'); // ['b', 'c']
The JS size property return the size of the Set.
const mySet = new Set(["a","b","c"]);
mySet.sizs; // 3
How you feel about this blog:
Share this blog on:
If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com
Contact UsWe are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..
Copyright ©
, Lynxsia IT Solutions, All rights reserved