Sets are the collection which store unique value .
Here is a table of the most common methods for JavaScript Set objects:
Method | Description |
add(value) | Adds a new element to the set |
delete(value) | Removes an element from the set |
has(value) | Returns true if the set contains the specified element, false otherwise |
clear() | Removes all elements from the set |
size | Returns the number of elements in the set |
Here is an example of how you can use these methods to manipulate a set:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p id="div1"></p> <br /> <p id="div2"></p> <script type="text/javascript"> const mySet = new Set(); mySet.add(1); mySet.add(2); mySet.add(3); console.log(mySet.has(2)); // Output: true mySet.delete(2); console.log(mySet.has(2)); // Output: false mySet.clear(); console.log(mySet.size); // Output: 0 </script> </body> </html> |
Console Output:
true
false
0