Class Checking: “instanceof”

Class Checking: “instanceof”

Class Checking in JavaScript with instanceof

The instanceof operator checks whether an object is an instance of a specific class or its parent class.

🔹 Basic instanceof Usage

The syntax is:

object instanceof Class

Returns true if object is an instance of Class (or a subclass), otherwise false.

Example: Checking an Instance

class Animal {} class Dog extends Animal {} const myDog = new Dog(); console.log(myDog instanceof Dog); // ✅ Output: true console.log(myDog instanceof Animal); // ✅ Output: true (because Dog extends Animal) console.log(myDog instanceof Object); // ✅ Output: true (everything in JS inherits from Object) console.log(myDog instanceof Array); // ❌ Output: false

myDog is an instance of both Dog and Animal due to inheritance.

🔹 Using instanceof with Built-in Classes

Example: Checking Built-in Types

console.log([] instanceof Array); // ✅ Output: true console.log([] instanceof Object); // ✅ Output: true (Array is a subclass of Object) console.log({} instanceof Object); // ✅ Output: true console.log(new Date() instanceof Date); // ✅ Output: true console.log(new Date() instanceof Object); // ✅ Output: true

✔ Arrays, Dates, and other objects inherit from Object.

🔹 instanceof with Custom Classes

You can use instanceof to check objects of user-defined classes.

Example: Custom Class Checking

class Person { constructor(name) { this.name = name; } } const user = new Person("Alice"); console.log(user instanceof Person); // ✅ Output: true console.log(user instanceof Object); // ✅ Output: true (all objects inherit from Object)

✔ Works the same as built-in classes!

🔹 Overriding instanceof with Symbol.hasInstance

You can customize how instanceof behaves using Symbol.hasInstance.

Example: Custom instanceof Behavior

class CustomCheck { static [Symbol.hasInstance](obj) { return obj.canFly === true; } } const bird = { canFly: true }; const cat = { canFly: false }; console.log(bird instanceof CustomCheck); // ✅ Output: true console.log(cat instanceof CustomCheck); // ❌ Output: false

✔ This makes instanceof return true only for objects with canFly: true.

🔹 instanceof vs. typeof

Featureinstanceoftypeof
Works with classes?✅ Yes❌ No
Works with primitives?❌ No✅ Yes
Works with built-in objects?✅ Yes✅ Yes (but limited)

Example: typeof vs. instanceof

console.log(typeof "hello"); // ✅ Output: "string" console.log("hello" instanceof String); // ❌ Output: false (primitive) console.log(new String("hello") instanceof String); // ✅ Output: true (object)

typeof is for primitives, instanceof is for objects.

🎯 Summary

instanceof checks if an object is an instance of a class or its parents.
✔ Works with custom classes and built-in objects (Array, Date, etc.).
✔ Can be overridden using Symbol.hasInstance.
✔ Use typeof for primitives (string, number, etc.).

🚀 Need help with instanceof? Let me know! 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close