Check if an Object is of type Map or Set in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
4 min

banner

# Table of Contents

  1. Check if an Object is of type Map in JavaScript
  2. Check if an Object is of type Set in JavaScript

# Check if an Object is of type Map in JavaScript

Use the instanceof operator to check if an object is a Map.

The instanceof operator will return true if the object is a Map and false otherwise.

index.js
const map = new Map(); map.set('site', 'bobbyhadz'); console.log(map instanceof Map); // ๐Ÿ‘‰๏ธ true console.log('test' instanceof Map); // ๐Ÿ‘‰๏ธ false console.log({} instanceof Map); // ๐Ÿ‘‰๏ธ false

check if object is of type map

The code for this article is available on GitHub

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('site', 'bobbyhadz'); console.log(map instanceof Map); // ๐Ÿ‘‰๏ธ true console.log(map instanceof CustomMap); // ๐Ÿ‘‰๏ธ true console.log({} instanceof CustomMap); // ๐Ÿ‘‰๏ธ false

also works if you have to extend map class

We subclassed the Map class 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.

# Using duck-typing to check if an object is a Map

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

using duck typing to check if object is map

The code for this article is available on GitHub

A simple way to think about duck-typing is that 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 provided argument 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: () => {}, }), );

If the object passed to the isMap function contains the properties/methods we are checking for, we would get back a false positive.

# Check if an Object is of type Set in JavaScript

Use the instanceof operator to check if an object is a Set.

The instanceof operator returns true if the prototype property of a constructor appears in the prototype chain of the object.

index.js
const set = new Set(); set.add('JavaScript'); console.log(set instanceof Set); // ๐Ÿ‘‰๏ธ true console.log('test' instanceof Set); // ๐Ÿ‘‰๏ธ false console.log({} instanceof Set); // ๐Ÿ‘‰๏ธ false

check if object is of type set in javascript

The code for this article is available on GitHub

We used the instanceof operator to check if the set variable has the prototype property of the Set() constructor in its prototype chain.

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

index.js
class CustomSet extends Set { example() { console.log('do work'); } } const set = new CustomSet(); set.add('JavaScript'); console.log(set instanceof Set); // ๐Ÿ‘‰๏ธ true console.log(set instanceof CustomSet); // ๐Ÿ‘‰๏ธ true console.log({} instanceof CustomSet); // ๐Ÿ‘‰๏ธ false

also works if you extend the set class

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

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

# Using duck-typing to check if an object is a Set

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

index.js
function isSet(set) { if ( set && typeof set.add === 'function' && typeof set.clear === 'function' && typeof set.delete === 'function' && typeof set.has === 'function' ) { return true; } return false; } const set = new Set(); console.log(isSet(set)); // ๐Ÿ‘‰๏ธ true const map = new Map(); console.log(isSet(map)); // ๐Ÿ‘‰๏ธ false console.log(isSet({})); // ๐Ÿ‘‰๏ธ false
The code for this article is available on GitHub

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

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

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

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

index.js
function isSet(set) { if ( set && typeof set.add === 'function' && typeof set.clear === 'function' && typeof set.delete === 'function' && typeof set.has === 'function' ) { return true; } return false; } // ๐Ÿ‘‡๏ธ true console.log( isSet({ add: () => {}, clear: () => {}, delete: () => {}, has: () => {}, }), );

If the object passed to the isSet() function contains the properties/methods we are checking for, we get back a false positive.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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 ยฉ 2024 Borislav Hadzhiev