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