JavaScript - Set.clear() Method
The Set.clear method in JavaScript is used to remove all the elements present in a set.
Syntax
Following is the syntax of JavaScript Set.clear() method −
clear()
Parameters
This method does not accept any parameters.
Return value
This method does not return any value instead, it modifies the original Set by removing all its elements.
Examples
Example
In the example below, we are using the JavaScript Set.clear() method to remove all elements present in the set −
<html>
<body>
<script>
const set = new Set();
set.add(5);
set.add(10);
set.add(12);
set.add(18);
document.write(`Result: ${[...set]} <br>`);
document.write("Size of set: ", set.size, "<br>");
set.clear();
document.write("Size of set after removing elements: ", set.size);
</script>
</body>
</html>
As we can see in the output, the size of the set has become 0.
Advertisements