What is the use of the Clear method in Sets in JavaScript ?
Last Updated :
01 Feb, 2024
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);
mySet.clear();
console.log(mySet);
|
Output
Set(5) { 1, 2, 3, 4, 5 }
Set(0) {}
Share your thoughts in the comments
Please Login to comment...