2

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?

Vendetta
  • 2,078
  • 3
  • 13
  • 31

2 Answers2

2

Use instanceof:

var map = new Map;
console.log(map instanceof Map); 
Ran Marciano
  • 1,431
  • 5
  • 13
  • 30
2
var foo = new Set;
foo instanceof Set; // True!
foo instanceof Map; // False!
Or Assayag
  • 5,662
  • 13
  • 57
  • 93