Suggest an editImprove this articleRefine the answer for “instanceof and prototypes”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The **`instanceof`** operator is directly tied to prototypes: it checks whether a constructor's `prototype` is somewhere in the object's prototype chain. **Key point:** it returns `true` if the prototype chain of `obj` contains the object `Constructor.prototype`, otherwise it returns `false`.Shown above the full answer for quick recall.Answer (EN)Imagethe `instanceof` operator is directly tied to **prototypes** and literally checks: > "Is the constructor's `prototype` somewhere in the object's prototype chain?" Let's break it down step by step. --- ## In short Syntax: ```javascript obj instanceof Constructor ``` Returns `true` if the **prototype chain of the object** `obj` contains the object `Constructor.prototype`. Otherwise it returns `false`. --- ## Example ```javascript function User() {} const tim = new User(); console.log(tim instanceof User); // true console.log(tim instanceof Object); // true console.log(tim instanceof Array); // false ``` Under the hood `instanceof` checks: ```javascript tim.__proto__ === User.prototype or tim.__proto__.__proto__ === User.prototype and so on until it reaches null ``` --- ## How it works "under the hood" ```javascript function instanceOf(obj, Constructor) { let proto = Object.getPrototypeOf(obj); // start from [[Prototype]] while (proto) { if (proto === Constructor.prototype) return true; // found a match proto = Object.getPrototypeOf(proto); // move up } return false; // reached null - no match } ``` This is exactly how `instanceof` walks the **prototype chain**, until it finds a match. --- ## Visually ```javascript function Animal() {} function Dog() {} Dog.prototype = Object.create(Animal.prototype); const rex = new Dog(); ``` Chain: ```javascript rex → Dog.prototype → Animal.prototype → Object.prototype → null ``` Checks: ```javascript console.log(rex instanceof Dog); // true console.log(rex instanceof Animal); // true console.log(rex instanceof Object); // true ``` Because all these prototypes appear in the `rex.__proto__` chain. --- ## Example with classes (same principle) ```javascript class Animal {} class Dog extends Animal {} const dog = new Dog(); console.log(dog instanceof Dog); // true console.log(dog instanceof Animal); // true console.log(dog instanceof Object); // true ``` The syntax is different, but under the hood it's the same prototype chain. --- ## Example where `instanceof` returns `false` ```javascript function User() {} const tim = new User(); Object.setPrototypeOf(tim, null); console.log(tim instanceof User); // false (the chain is broken) ``` If an object **has no prototype**, or the chain does not contain `User.prototype`, the operator returns `false`. --- ## The catch with different contexts (for example, iframes) ```javascript const arr = []; console.log(arr instanceof Array); // true // but in a different window/iframe: console.log(arr instanceof window.top.Array); // false ``` Because each context (frame, realm) has its own `Array.prototype`, and `instanceof` compares the **object by reference**, not by name. --- ## Alternative: `obj.constructor` ```javascript const user = new User(); console.log(user.constructor === User); // true ``` But this is **unreliable**: the `constructor` property can be overridden: ```javascript user.constructor = null; console.log(user instanceof User); // still true ``` That's why `instanceof` is the **safer** approach. --- ## Summary | Check | What it does | Uses | |---|---|---| | `obj instanceof Constructor` | Checks whether `Constructor.prototype` is in the object's prototype chain | `[[Prototype]]` | | Returns | `true` or `false` | | | More reliable than `obj.constructor` | Yes | | | Works with classes | Yes | | | Sensitive to context (iframe) | Yes | | --- ## Visual scheme ```javascript obj ↑ __proto__ → Constructor.prototype ↑ __proto__ → Object.prototype ↑ null ``` If `Constructor.prototype` appears on this path, `instanceof` returns `true`. --- **In one sentence:** > `instanceof` checks whether the constructor's `prototype` **is part of** the object's prototype chain, > walking up through `__proto__` until it finds a match or `null`.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.