Example 1 — Custom classes
Works with class constructors.
JavaScript
class Dog {}
console.log(new Dog() instanceof Dog); How It Works
- Instance links to Dog.prototype.
value instanceof Constructor is true when Constructor.prototype is in value’s prototype chain. Useful for class instances and built-ins like Date or Array (prefer Array.isArray for arrays).
console.log([] instanceof Array);
console.log(new Date() instanceof Date);
console.log({} instanceof Array); Works with class constructors.
class Dog {}
console.log(new Dog() instanceof Dog); null/undefined throw or are false safely with care.
console.log(null instanceof Object); Numbers are not Number objects.
console.log(3 instanceof Number);
console.log(Object(3) instanceof Number); CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful