JavaScript Map clear() method - JavaScript Tutorial

JavaScript Map clear() method

BLUF: This tutorial on JavaScript Map clear() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Map clear() method

Understanding JavaScript Map clear() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience