JavaScript Map clear() method

The clear method of the JavaScript Map object is utilized to eliminate all entries from the Map instance.

Syntax

The syntax for the clear method is as follows:

Example

mapObj.clear()

JavaScript Map clear method example

In this section, we will explore the clear method by examining several examples.

Example 1

Let's see a simple example of clear method.

Example

<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:

Output

Size before invoking clear(): 3

Size after invoking clear(): 0

Example 2

Let’s examine an example to ascertain if the map object includes the designated element.

Example

<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:

Output

Element present before invoking clear(): true

Element present after invoking clear(): false

Input Required

This code uses input(). Please provide values below: