➕instanceof
`obj instanceof SomeClass`
JS ⟩ statement ⟩ expression ⟩ operator ⟩ relational ⟩ instanceof
check if SomeClass.prototype is in the prototype chain of obj.
obj instanceof SomeClassreplit: "instanceof"
let d = new Date();
let a = [1, 2, 3];
let o = Object.create(null); // ⭐ pure object
function F(){}
d instanceof Date, // true
d instanceof Object, // true
d instanceof Number, // false
a instanceof Array, // true
a instanceof Object, // true
a instanceof RegExp, // false
o instanceof Object, // false❗
1 instanceof Object, // false (1: not an object)
{} instanceof F, // false
{} instanceof {}, // ⛔ TypeError
// ^^
// TypeError: Right-hand side of 'instanceof' is not callable
{} instanceof 8, // ⛔ TypeError
// ^
// TypeError: Right-hand side of 'instanceof' is not an objectLast updated
Was this helpful?