Check if Object is of type Map in JavaScript

avatar

Borislav Hadzhiev

Last updated: Oct 19, 2021

banner

Photo from Unsplash

Check if Object is of type Map in JavaScript #

Use the instanceof operator to check if an object is a Map - myObj instanceof Map. The instanceof operator returns true if the prototype property of a constructor appears in the prototype chain of the object.

index.js
const map = new Map(); map.set('name', 'Tom'); console.log(map instanceof Map); // 👉️ true console.log('test' instanceof Map); // 👉️ false console.log({} instanceof Map); // 👉️ false

We used the instanceof operator to check if the map variable has the prototype property of the Map constructor in its prototype chain.

This approach would also work if you extend the Map class.

index.js
class CustomMap extends Map { myMethod() { console.log('do work'); } } const map = new CustomMap(); map.set('name', 'Tom'); console.log(map instanceof Map); // 👉️ true console.log(map instanceof CustomMap); // 👉️ true console.log({} instanceof CustomMap); // 👉️ false

In this example, we extended the Map object and the instanceof operator returned true for both Map and CustomMap.

Using the instanceof operator is risky when working with IFrames (inline frames). The instanceof check might not work when performed in a different window context, especially on some older browsers.

As an alternative, you can use duck-typing to check if the object is a Map.

index.js
function isMap(map) { if ( map && typeof map.clear === 'function' && typeof map.delete === 'function' && typeof map.get === 'function' && typeof map.has === 'function' && typeof map.set === 'function' ) { return true; } return false; } const map = new Map(); console.log(isMap(map)); // 👉️ true const set = new Set(); console.log(isMap(set)); // 👉️ false console.log(isMap({})); // 👉️ false

A simple way to think about duck-typing is - we're basically saying:

A Map has the following properties / methods. If an object also has these properties/methods, then it must be a Map.

In our isMap function we check if the passed in parameter has the methods a Map would have, if the condition is met, we return true.

This could go wrong if an object that is not a Map, but contains these same methods gets passed to the function.

index.js
function isMap(map) { if ( map && typeof map.clear === 'function' && typeof map.delete === 'function' && typeof map.get === 'function' && typeof map.has === 'function' && typeof map.set === 'function' ) { return true; } return false; } // 👇️ true console.log( isMap({ clear: () => {}, delete: () => {}, get: () => {}, has: () => {}, set: () => {}, }), );

The duck-typing approach is not bullet proof either. If the object passed to the isMap function contains the properties/methods we are checking for, we get back a false positive.

Further Reading #

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.