Set

4 0 0
                                        

Set is type of collection where the values are not allowed to duplicate and it doesn't use keys.

Methods of Set

1. let set = new Set() - creates an empty set.

2. let set = new Set([1, 2, 3, 4, 5]) - creates a set where the values is get from the array and the duplicates is automatically remove.

3. let set = new Set(Iterable) - creates a set where the values comes from the iterable objects.

4. set.add(value) - adding the value to the set.

5. set.delete(value) -  deleting the value from the set.

6. set.has(value) - returns true if the value does exist.

7. set.clear() - deletes everything from the set.

8. set.size() - returns the size or the length or the total number of elements in the set.

9. set.forEach(function(value1, value2, set){//statement}) - to display each element in a set without using a loop.

10. set.keys() - it returns all the keys which is the copy of its values acting as keys.

11. set.values() - it returns all the values

12. set.entries() - it returns both the keys and the values in pairs

==================

let set1 = new Set([1, 2, 4, 4, 10, 5, 10, 6]);

function displayTheSet(){

    for(let x of set1){

        document.writeln(x);

    }

        document.writeln("*****");

    set1.forEach((value1, _, set1) => {document.writeln(value1);});

       document.writeln("*****");

    for(let x of set1.entries()){

        document.writeln(x);

    }

}

==================

HTML:

displayTheSet()

Result:

1 2 4 10 5 6 ***** 1 2 4 10 5 6 ***** 1,1 2,2 4,4 10,10 5,5 6,6

Javascript ProgrammingWhere stories live. Discover now