Suggest an editImprove this articleRefine the answer for “The instanceof operator?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**The `instanceof` operator** in JavaScript is used to check whether an object is an **instance** of a given **class or constructor**, that is, which "prototype chain" it belongs to. **Key point:** `instanceof` checks whether `Constructor.prototype` appears in the object's prototype chain and accounts for inheritance, but it does not work with primitives - for arrays, `Array.isArray()` is more reliable.Shown above the full answer for quick recall.Answer (EN)ImageThe `instanceof` operator in JavaScript is used to check whether an object is an **instance** of a given **class or constructor**. In other words, it checks **which "prototype chain" the object belongs to.** --- ## Syntax ```javascript object instanceof Constructor ``` - `object` - the object to check. - `Constructor` - a constructor function or a class. > Returns: `true` or `false` --- ## Simple example ```javascript class User {} const user = new User(); console.log(user instanceof User); // true console.log(user instanceof Object); // true (all objects inherit from Object) ``` > `user` is an instance of `User` > And also `user` is an instance of `Object`, because `User` inherits from `Object` --- ## How this works under the hood `instanceof` checks the **prototype chain**: - takes `object.__proto__` - compares it with `Constructor.prototype` - if it does not match, it moves up the prototype chain (`object.__proto__.__proto__`, and so on) - if a match is eventually found → `true`, otherwise → `false` --- ### Illustration: ```javascript function Animal() {} const dog = new Animal(); // Check: console.log(dog instanceof Animal); // true // Under the hood: console.log(Object.getPrototypeOf(dog) === Animal.prototype); // true ``` --- ## Example with inheritance ```javascript class Animal {} class Dog extends Animal {} const rex = new Dog(); console.log(rex instanceof Dog); // true console.log(rex instanceof Animal); // true console.log(rex instanceof Object); // true console.log(rex instanceof Array); // false ``` > `instanceof` "climbs" the entire inheritance chain: > `Dog → Animal → Object` --- ## Example with regular constructor functions ```javascript function Car() {} function Bike() {} const tesla = new Car(); console.log(tesla instanceof Car); // true console.log(tesla instanceof Bike); // false ``` --- ## Example with arrays ```javascript const arr = [1, 2, 3]; console.log(arr instanceof Array); // true console.log(arr instanceof Object); // true ``` > All arrays are objects, so `arr instanceof Object` is also `true`. --- ## Important: `instanceof` does not work with primitives ```javascript console.log(123 instanceof Number); // false console.log('hello' instanceof String); // false ``` > Because `123` and `'hello'` are **primitives**, not objects. > (But `new Number(123)` will be an object.) --- ## Example with wrapper objects ```javascript const n = new Number(123); console.log(n instanceof Number); // true const s = new String('hi'); console.log(s instanceof String); // true ``` > But in real code, wrappers (`new Number`, `new String`) are almost never used, > it's better to use primitives. --- ## Custom checks Sometimes `instanceof` won't work if the object came from **a different context (iframe, window)**, because it has **its own** `Array` **prototype**. Example: ```javascript iframe.contentWindow.Array !== window.Array ``` > So `instanceof` can return `false`, > even if it *really is an array*. In such cases, it's better to use: ```javascript Array.isArray(value); ``` --- ## In short | Checks | Whether the object is in the constructor's prototype chain | |---|---| | Returns | `true` / `false` | | Works with | Objects (not primitives) | | Checks against | `Constructor.prototype` | | Accounts for inheritance | Yes | | Suitable for arrays, classes, functions | Yes | --- ## Reinforcement example ```javascript class Person {} class Developer extends Person {} const tim = new Developer(); console.log(tim instanceof Developer); // true console.log(tim instanceof Person); // true console.log(tim instanceof Object); // true console.log(tim instanceof Array); // false ```For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.