What is the use of the Clear method in Sets in JavaScript ?
The clear()
method in JavaScript Sets is used to remove all elements from a Set, effectively making the Set empty. It does not take any arguments and does not return any value. After calling the clear()
, the Set will have a size of 0.
Example: Here, the clear()
method is called on the mySet
Set, removing all elements and leaving an empty Set.
Javascript
let mySet = new Set([1, 2, 3, 4, 5]); console.log(mySet); // Output: Set { 1, 2, 3, 4, 5 } // Clear all elements from the Set mySet.clear(); console.log(mySet); // Output: Set {} |
Output
Set(5) { 1, 2, 3, 4, 5 } Set(0) {}
Contact Us