C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Set clear() methodThe JavaScript Set clear() method is used to remove all the elements from Set object. SyntaxThe clear() method is represented by the following syntax: setObj.clear() JavaScript Set clear() method exampleHere, we will understand clear() method through various examples. Example 1Let's see a simple example of clear() method. <script> var set = new Set(); set.add("jQuery"); set.add("AngularJS"); set.add("Bootstrap"); document.writeln("Size before invoking clear() method: "+ set.size+"<br>"); set.clear(); document.writeln("Size after invoking clear() method: "+set.size); </script> Output: Size before invoking clear() method: 3 Size after invoking clear() method: 0 Example 2Let's see an example to determine whether the set object contains the specified element. <script> var set = new Set(); set.add("jQuery"); set.add("AngularJS"); set.add("Bootstrap"); document.writeln("Element present before invoking clear() method: "+ set.has("Bootstrap")+"<br>"); set.clear(); document.writeln("Element present after invoking clear() method: "+set.has("Bootstrap")); </script> Output: Element present before invoking clear() method: true Element present after invoking clear() method: false
Next TopicJavaScript Set delete() Method
|