JavaScript Map clear() method

The JavaScript map clear() method eliminates all the elements from a Map object.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mapObj.clear()
mapObj.clear()
mapObj.clear()

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var hello=new Map();
hello.set("GOOD MORNING");
hello.set("GOOD AFTERNOON");
hello.set("GOOD EVENING");
hello.set("GOOD NIGHT");
document.writeln("Number of elements: "+ hello.size+"<br>");
hello.clear();
document.writeln("Number of elements: "+hello.size);
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var hello=new Map(); hello.set("GOOD MORNING"); hello.set("GOOD AFTERNOON"); hello.set("GOOD EVENING"); hello.set("GOOD NIGHT"); document.writeln("Number of elements: "+ hello.size+"<br>"); hello.clear(); document.writeln("Number of elements: "+hello.size); </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<script>
var hello=new Map();
hello.set("GOOD MORNING");
hello.set("GOOD AFTERNOON");
hello.set("GOOD EVENING");
hello.set("GOOD NIGHT");
document.writeln("Number of elements: "+ hello.size+"<br>");
hello.clear();
document.writeln("Number of elements: "+hello.size);
</script>
</body>
</html>