Skip to main content

How to Check if an Object is a Map or a Set in JavaScript

When working with different data structures in JavaScript, you often need to verify an object's type before you can safely use its specific methods. For example, you can't use .get() on a Set or .add() on a Map. Distinguishing between these collection types and plain objects is essential for writing robust and error-free code.

This guide will teach you the modern, standard methods for type-checking Map and Set objects. You will learn how to use the instanceof operator for most cases and understand an alternative, more robust method using Object.prototype.toString for situations where instanceof can fail.

The Core Method: The instanceof Operator

For the vast majority of use cases, the instanceof operator is the simplest and most readable way to check if an object is an instance of Map, Set, or any other class.

The instanceof operator checks the prototype chain of an object. It returns true if the prototype property of a constructor (like Map or Set) appears anywhere in the object's prototype chain.

Problem: given a set of different variables, how can we identify which ones are Maps or Sets?

// Problem: Identify the type of these variables.
const myMap = new Map([['key', 'value']]);
const mySet = new Set([1, 2, 3]);
const myObject = { key: 'value' };
const myString = 'hello';

The Solution is to use the instanceof operator that provides a clear boolean answer.

const myMap = new Map();
const mySet = new Set();
const myObject = {};

// --- Checking for Map ---
console.log(myMap instanceof Map); // Output: true
console.log(myObject instanceof Map); // Output: false

// --- Checking for Set ---
console.log(mySet instanceof Set); // Output: true
console.log(myObject instanceof Set); // Output: false

This method also works correctly with subclasses. If you create a CustomMap that extends Map, an instance of CustomMap will also be an instanceof Map.

class CustomMap extends Map { /* ... */ }
const customMapInstance = new CustomMap();

console.log(customMapInstance instanceof CustomMap); // Output: true
console.log(customMapInstance instanceof Map); // Output: true

An Alternative Method: Object.prototype.toString

The instanceof operator can fail in some edge cases, particularly when dealing with objects from different JavaScript environments (like iframes or web workers), as each environment has its own Map and Set constructor.

A more robust method that works across these different "realms" is to use Object.prototype.toString. This method returns a string that specifies the internal [[Class]] of an object.

Solution:

function isMap(value) {
return Object.prototype.toString.call(value) === '[object Map]';
}

function isSet(value) {
return Object.prototype.toString.call(value) === '[object Set]';
}

// Example Usage:
const myMap = new Map();
const mySet = new Set();

console.log(isMap(myMap)); // Output: true
console.log(isMap(mySet)); // Output: false

console.log(isSet(mySet)); // Output: true
console.log(isSet(myMap)); // Output: false
note

This method is considered the most reliable for accurately identifying built-in object types in JavaScript, although it is more verbose than instanceof.

The "Duck Typing" Method and Its Flaws

Another approach you might see is "duck typing." The idea is: "If it walks like a duck and quacks like a duck, then it must be a duck." In code, this means checking if an object has the methods you expect a Map or Set to have.

Problem with Duck Typing: this method is brittle and can lead to false positives. A plain object can be crafted to have the same method names as a Map or Set, which would fool the check.

function isMap(obj) {
return obj && typeof obj.get === 'function' && typeof obj.set === 'function';
}

const fakeMap = {
get: () => 'I am not a real map',
set: () => {}
};

// This check is fooled and returns true, which is incorrect.
console.log(isMap(fakeMap)); // Output: true

Because this method is not reliable, it should be avoided in favor of instanceof or Object.prototype.toString.

Conclusion

Verifying if an object is a Map or a Set is a simple task in JavaScript if you use the right tools.

  • For most everyday scenarios, the instanceof operator is the best choice. It is clean, readable, and works perfectly within a single JavaScript environment.
    • myVar instanceof Map
    • myVar instanceof Set
  • For highly robust code that must work across different frames or windows, the Object.prototype.toString.call() method is the most accurate and reliable solution.

You should avoid duck typing (checking for method names), as it can be easily fooled and lead to incorrect results.