How can I check if a value is of type Map in JavaScript?

Viewed 753

I have a variable that could be an Object, a Map, or neither. I can easily check for objects with typeof, but I need to conditionally Map.map() the variable if it is a Map, and typeof doesn't work with maps. Any suggestions?

2 Answers

Use instanceof:

var map = new Map;
console.log(map instanceof Map); 

var foo = new Set;
foo instanceof Set; // True!
foo instanceof Map; // False!
Related