JavaScript Map has() Method Tutorial

In this section, we will learn what the has() method is and how to use it in JavaScript.

What is JavaScript Map has() Method?

We use the method `has()` to check and see if the target map has a key that we set as the argument of this method.

Basically, the method takes one argument and that is the key in which we want to search for it on the target map.

If the map has such key, the return value will be true otherwise, the value false will return.

JavaScript Map has() Method Syntax:

has(key)

has() Function Parameters:

The method takes one argument and that is the key we want to check the target Map object for.

has() Function Return Value:

The return value of this method is a boolean.

If the target map object has the key (that we set as the argument of this method) the return value becomes true.

Otherwise, the return value will be false.

Example: using Map has() 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");

if (map.has(true)){
  console.log(`Yes, the map has the key 'true' and its value is: ${map.get(true)}`);
}

Output:

Yes, the map has the key 'true' and its value is: false

Leave a Reply