JavaScript Map clear() Method Tutorial

What is JavaScript Map clear() Method?

We use the `clear()` method when we want to remove the entire entities in a map object.

Note: entity means a pair of key-value.

The method does not take any argument and we simply use the target map to call this method.

JavaScript Map clear() Method Syntax:

map.clear();

clear() Function Parameters:

The method does not take an argument.

clear() Function Return Value:

The return value of this method is undefined.

Example: using Map clear() method in JavaScript

const map = new Map();

map.set("John","222-22222222");

map.set(22 , 2000);

map.set(true, false);

map.set("Jack", "333-3234232");

console.log(`The size of the map is: ${map.size}`);

map.clear();

console.log(`The size of the map after running the clear() method: ${map.size}`);

Output:

The size of the map is: 4

The size of the map after running the clear() method: 0

Leave a Reply