61

What are alternative / better ways to check if a JavaScript object is Map or Set than:

Object.getPrototypeOf(map) === Map.prototype
Object.getPrototypeOf(set) === Set.prototype
krl
  • 5,087
  • 4
  • 36
  • 53

1 Answers1

116

Use instanceof:

var foo = new Set;
foo instanceof Set; // True!
foo instanceof Map; // False!
amphetamachine
  • 27,620
  • 12
  • 60
  • 72