47

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
  • 4,819
  • 4
  • 32
  • 52

1 Answers1

92

Use instanceof:

var foo = new Set;
foo instanceof Set; // True!
foo instanceof Map; // False!
amphetamachine
  • 25,180
  • 10
  • 57
  • 71