Map.has( ) In JavaScript

What is a Map in JavaScript ?

  • Map is a data structure in JavaScript which allows storing of [key, value] pairs where any value can be either used as a key or value.
  • The keys and values in the map collection may be of any type and if a value is added to the map collection using a key which already exists in the collection, then the new value replaces the old value.
  • The iteration of elements in a map object is done in the insertion order and a “for…” loop returns an array of all [key, value] pairs for each iteration.

Differences between Objects and Maps in JavaScript
Both of these data structures are similar in many ways such as both store values using keys, allow retrieval of those values using keys, deletion of keys and verify whether a key holds any value or not. However, there are quite significant differences between Objects and Maps in JavaScript which make the usage of maps a better and more preferable option in many cases.

  • The keys used in maps can be any type of values such as functions, objects etc whereas the keys in objects are limited to symbols and strings.
  • The size of a map can be known easily by using the size property but while dealing with objects, the size has to be determined manually.
  • A Map should be preferred in cases where the requirement involves frequent addition and removal of [key, value] pairs because a map is an iterative data type and can be directly iterated whereas iterating an object requires obtaining its keys in a specific manner.

Map.has() Method in JavaScript
The Map.has() method in JavaScript is used to check whether an element with a specified key exists in a map or not. It returns a boolean value indicating the presence or absence of an element with a specified key in a map.
The Map.has() method takes the key of the element to be searched as an argument and returns a boolean value. It returns true if the element exists in the map else it returns false if the element doesn’t exist.



Applications:

  • Map.has() Method can be used to check whether an element with a specified key exists in a map or not. .

Syntax:

mapObj.has(key)

Parameters Used:

  • key: It is the key of the element of the map which has to be searched.
  • Return Value:

  • The Map.has() method returns a boolean value. It returns true if the element exists in the map else it returns false if the element doesn’t exist.

Examples of the above function are provided below.

Examples:

Input : var myMap = new Map();
        myMap.set(0, 'geeksforgeeks');
        console.log(myMap.has(0));
        
Output: true

Explanation: In this example, a map object “myMap” has been created with a single [key, value] pair and the Map.has() method is used to check whether an element with the key ‘0’ exists in the map or not.

Input : var myMap = new Map();
        myMap.set(0, 'geeksforgeeks');
        myMap.set(1, 'is an online portal');
        myMap.set(2, 'for geeks');
        console.log(myMap.has(0));
        console.log(myMap.has(3));

Output: true
        false

Explanation: In this example, a map object “myMap” has been created with three [key, value] pairs and the Map.has() method is used to check whether an element with the key ‘0’ and ‘3’ exists in the map or not.

Code 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // creating a map object
    var myMap = new Map();
  
// Adding [key, value] pair to the map
myMap.set(0, 'geeksforgeeks');
  
// displaying whether an element with the key '0' exists in the map or not
// using Map.has() method
console.log(myMap.has(0));
  
< /script>

chevron_right


OUTPUT :

true

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // creating a map object
    var myMap = new Map();
  
// Adding [key, value] pair to the map
myMap.set(0, 'geeksforgeeks');
myMap.set(1, 'is an online portal');
myMap.set(2, 'for geeks');
  
// displaying whether an element with the key '0' and '3' exists in
// the map or not using Map.has() method
console.log(myMap.has(0));
console.log(myMap.has(3));
  
< /script>

chevron_right


OUTPUT :

true
false

Reference :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.