Skip to content

JavaScript Set clear() | Method

  • by

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

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading