Suggest an editImprove this articleRefine the answer for “Checking for a class instance”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The main way to check that an object is an instance of a class is the `obj instanceof Class` operator: it checks whether a reference to `Class.prototype` exists in the object's **prototype chain**, and returns `true` even for subclasses thanks to inheritance. **Key point:** alternatives like `obj.constructor === Class` or `Object.getPrototypeOf(obj) === Class.prototype` are less reliable (the first breaks if `prototype` was changed, the second doesn't climb the inheritance chain), and for checking arrays across different contexts (iframe, Worker) you should use `Array.isArray()` instead of `instanceof`.Shown above the full answer for quick recall.Answer (EN)Image## 1. The `instanceof` Operator: The Main Way ```javascript class User {} const u = new User(); console.log(u instanceof User); // true ``` `instanceof` checks whether the **prototype chain** of the object `u` contains a reference to `User.prototype`. --- ### Example: ```javascript class Animal {} class Dog extends Animal {} const dog = new Dog(); console.log(dog instanceof Dog); // true console.log(dog instanceof Animal); // true: inheritance works console.log(dog instanceof Object); // true: all objects inherit from Object ``` `instanceof` walks **up the prototype chain**, and if it finds a match with `Class.prototype`, it returns `true`. --- ## 2. How `instanceof` Works Under the Hood ```javascript obj instanceof Class ``` is roughly equivalent to this code: ```javascript let proto = Object.getPrototypeOf(obj); while (proto) { if (proto === Class.prototype) return true; proto = Object.getPrototypeOf(proto); } return false; ``` In other words, `instanceof` simply **checks whether** `Class.prototype` **is present in the** `__proto__` **chain**. --- ## 3. An Example Where Inheritance Affects the Result ```javascript class A {} class B extends A {} class C extends B {} const c = new C(); console.log(c instanceof A); // true console.log(c instanceof B); // true console.log(c instanceof C); // true ``` The prototype chain: ```javascript c → C.prototype → B.prototype → A.prototype → Object.prototype → null ``` `instanceof` walks sequentially up the chain. --- ## 4. Checking Without `instanceof`: Through `constructor` You can check whether an object's constructor matches the class: ```javascript class User {} const u = new User(); console.log(u.constructor === User); // true ``` But: - This approach breaks if the object was created via `Object.create()`. - Or if someone changed `prototype.constructor`. ```javascript const fake = Object.create(User.prototype); console.log(fake.constructor === User); // true console.log(fake instanceof User); // true ``` > Both approaches work as long as the prototype chain isn't broken. --- ## 5. An Alternative: Through `Object.getPrototypeOf` ```javascript class User {} const u = new User(); console.log(Object.getPrototypeOf(u) === User.prototype); // true ``` This works **only for a direct instance**. If there's inheritance, this method doesn't climb higher up the chain (unlike `instanceof`). --- ## 6. Checking Built-in Classes (Array, Date, Error, Map, etc.) ```javascript console.log([] instanceof Array); // true console.log([] instanceof Object); // true console.log(new Date() instanceof Date); // true console.log(new Date() instanceof Object); // true ``` `instanceof` works with **built-in classes** exactly the same way, since they are also built on prototypes. --- ## 7. Important When Working Across Different "Realms" (iframe, Worker, etc.) If an object was created in **a different context (for example, a different iframe)**, its `Array` or `Object` is **a different constructor**, so `instanceof` may return `false`. ```javascript // In a different iframe: console.log(arr instanceof window.Array); // false ``` The solution: use ```javascript Array.isArray(arr); // safe even across contexts ``` --- ## 8. An Example With a Static Check Method Sometimes a class gets **its own check method**: ```javascript class User { static isUser(obj) { return obj instanceof User; } } const u = new User(); console.log(User.isUser(u)); // true ``` Such a method is useful in large codebases, where types are often checked centrally. --- ## Summary | Method | Example | What It Checks | Supports Inheritance | Note | | --- | --- | --- | --- | --- | | `instanceof` | `obj instanceof Class` | The `__proto__` chain | Yes | Reliable | | `constructor` | `obj.constructor === Class` | The object's constructor | Partially | Breaks if `prototype` was changed | | `Object.getPrototypeOf` | `Object.getPrototypeOf(obj) === Class.prototype` | The direct prototype | No | Only for the direct class | | Static method | `Class.isInstance(obj)` | Any logic | Yes | Convenient in large projects | --- **In short:** > You can check that an object is an instance of a class like this: > > ```javascript > obj instanceof Class > ``` > > This operator: > > - walks the prototype chain (`__proto__`); > - supports inheritance; > - works with built-in classes too (`Array`, `Date`, `Error`); > - but can be unreliable across contexts (iframe, Worker).For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.