JS Built-ins
Operatorsadvanced

Operators β€” in and instanceof

in checks property existence, instanceof checks prototype chain

  • 'key' in obj β†’ true if the object has the key (own or inherited)
  • obj instanceof Constructor β†’ true if Constructor.prototype is in the prototype chain

Challenge: Write checkMembership() that tests:

  • hasName: 'name' in { name: 'Alice' }
  • hasFoo: 'foo' in { name: 'Alice' }
  • isArray: [1,2,3] instanceof Array
  • isDate: new Date() instanceof Date
πŸ’‘ Hints (2)
  • β€’ in checks for own AND inherited properties
  • β€’ instanceof traverses the prototype chain