Check if a Key exists in a Map using JavaScript

avatar

Borislav Hadzhiev

3 min

banner

Photo from Unsplash

Table of Contents #

  1. Check if a Key exists in a Map in JavaScript
  2. Check if a Map has an Object Key in JavaScript

Check if a Key exists in a Map in JavaScript #

Use the Map.has() method to check if a key exists in a Map object.

The has() method returns true if the specified key exists in the Map, otherwise, it returns false.

index.js
const map = new Map(); map.set('name', 'bobbyhadz'); console.log(map.has('name')); // ๐Ÿ‘‰๏ธ true console.log(map.has('age')); // ๐Ÿ‘‰๏ธ false if (map.has('name')) { // ๐Ÿ‘‡๏ธ this runs console.log('The `name` key exists in the Map object'); } else { console.log('The `name` key does NOT exist in the Map object'); }
If you need to check if an object key exists in a Map, scroll down to the next subheading.

The only parameter the Map.has() method takes is the key of the element to test for presence in the Map.

The Map.has() method returns a boolean result:

  • true if the key exists in the Map object
  • false if the key doesn't exist in the Map object
The has() method returns true if the key is set to undefined, null or any other falsy value.
index.js
const map = new Map(); map.set('undef', undefined); map.set('null', null); map.set('nan', Number.NaN); console.log(map.has('undef')); // ๐Ÿ‘‰๏ธ true console.log(map.has('null')); // ๐Ÿ‘‰๏ธ true console.log(map.has('nan')); // ๐Ÿ‘‰๏ธ true

The method checks for existence, not whether the value is truthy or falsy.

If a value gets deleted from the map, the has method picks up the update immediately.

index.js
const map = new Map(); map.set('name', 'Tom'); console.log(map.has('name')); // ๐Ÿ‘‰๏ธ true map.delete('name'); console.log(map.has('name')); // ๐Ÿ‘‰๏ธ false

We used the Map.delete() method to remove an element from the Map.

As expected, the call to the has() method returned false immediately after.

The same is the case when using the Map.set() method to add an element to the Map or the map.clear() method to remove all elements from the Map.

index.js
const map = new Map(); console.log(map.has('name')); // ๐Ÿ‘‰๏ธ false map.set('name', 'bobbyhadz'); console.log(map.has('name')); // ๐Ÿ‘‰๏ธ true map.clear(); console.log(map.has('name')); // ๐Ÿ‘‰๏ธ false

The Map.clear() method removes all elements from a Map object, so passing any key to the Map.has() method returns false after a call to Map.clear().

Check if a Map has an Object Key in JavaScript #

To check if a Map has an object key, call the has() method, passing it a reference to the object, e.g. map.has(obj).

The has() method will return true if the object key is contained in the Map and false otherwise.

index.js
const obj = {country: 'Chile'}; const map1 = new Map([[obj, {city: 'Santiago'}]]); // โœ… ( BEST ) - With reference console.log(map1.has(obj)); // ๐Ÿ‘‰๏ธ true console.log(map1.get(obj)); // ๐Ÿ‘‰๏ธ {city: 'Santiago'} // -------------------------------------------------------- // โœ… If you don't have a reference, loop over the map to check let hasKey = false; for (const [key, value] of map1) { if (typeof key === 'object' && key.country === 'Chile') { hasKey = true; break; } } console.log(hasKey); // ๐Ÿ‘‰๏ธ true

We used the Map.has method to check if a Map has an object key.

Note that we passed the object by reference to the has() method.

This wouldn't work if you pass an object containing the same key-value pairs because it would be stored at a different location in memory.

index.js
const obj = {country: 'Chile'}; const map1 = new Map([[obj, {city: 'Santiago'}]]); console.log(map1.has({country: 'Chile'})); // ๐Ÿ‘‰๏ธ false

JavaScript objects are compared by reference, and not by their contents.

index.js
const obj = {country: 'Chile'}; // ๐Ÿ‘‡๏ธ๏ธ false console.log({country: 'Chile'} === {country: 'Chile'}); // ๐Ÿ‘‡๏ธ๏ธ true console.log(obj === obj);

Check if an Object key exists in a Map without a reference #

If you don't have a reference to the object, use a for...of loop to iterate over the Map and check for the existence of the object key.

index.js
const map1 = new Map([[{country: 'Chile'}, {city: 'Santiago'}]]); let hasKey = false; for (const [key, value] of map1) { if (typeof key === 'object' && key.country === 'Chile') { hasKey = true; break; } } console.log(hasKey); // ๐Ÿ‘‰๏ธ true

The for...of statement is used to loop over iterable objects like arrays, strings, Map, Set and NodeList objects and generators.

On each iteration, we check if the current key is an object and contains a country property that is equal to Chile.

You don't have to destructure the value variable if you don't need it.

After we have confirmed that the object key exists in the Map, we use the break statement to exit the loop and avoid unnecessary work.

We used the logical AND (&&) operator, so for the if block to run, both conditions have to be met.

The key has to be an object and it has to have a country property that is equal to Chile.

You can check for multiple properties and values to make your if statement more robust.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev