The JavaScript Set clear() method removes all elements from an object and makes it empty. You don’t need to pass any parameters to the Set.clear() method.
setObj.clear()
JavaScript Set clear()
Simple example code.
<!DOCTYPE html>
<html>
<body>
<script>
const set1 = new Set([1,20,3,40]);
console.log(set1.size);
set1.clear()
console.log(set1.size)
console.log(set1)
</script>
</body>
</html>
Output:
![JavaScript Set clear() Method](https://i0.wp.com/tutorial.eyehunts.com/wp-content/uploads/2022/08/JavaScript-Set-clear-Method.jpg?resize=182%2C90&ssl=1)
More examples
const set1 = new Set();
set1.add(1);
set1.add('foo');
console.log(set1.size); // 2
set1.clear();
console.log(set1.size); // 0
Do comment if you have any doubts or suggestions on this Js set method topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version