instanceof - JavaScript | MDN

developer.mozilla.org

Tin mới

HTML: Markup language

See all HTML references

Date & time formats

See all HTML guides

CSS: Styling language

See all CSS references

See all CSS guides

Centering an element

JavaScript: Scripting language

Standard built-in objects

Expressions & operators

Statements & declarations

See all JavaScript references

Control flow & error handing

Loops and iteration

Working with objects

See all JavaScript guides

Web APIs: Programming interfaces

Service worker API

Using the Web animation API

Using the Fetch API

Working with the History API

Using the Web speech API

All web technology

Progressive web apps

Learn web development

Getting started modules

Structuring content with HTML module

CSS styling basics module

Dynamic scripting with JavaScript module

Border-image generator

Border-radius generator

Box-shadow generator

Color format converter

Symbol.hasInstance

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value. Its behavior can be customized with Symbol.hasIns

Object.setPrototypeOf

The instanceof operator tests the presence of constructor.prototype in object's prototype chain. This usually (though not always) means object was constructed with constructor.

Function.prototype[Symbol.hasInstance]()

If constructor has a Symbol.hasInstance method, the method will be called in priority, with object as its only argument and constructor as this.

instanceof and multiple realms

JavaScript execution environments (windows, frames, etc.) are each in their own realm. This means that they have different built-ins (different global object, different constructors, etc.). This may result in unexpected

instanceof and multiple realms

JavaScript execution environments (windows, frames, etc.) are each in their own realm. This means that they have different built-ins (different global object, different constructors, etc.). This may result in unexpected

instanceof and multiple realms

JavaScript execution environments (windows, frames, etc.) are each in their own realm. This means that they have different built-ins (different global object, different constructors, etc.). This may result in unexpected

Objects created using Object.create()

The following example shows the behavior of instanceof with objects created using Object.create().

Overriding the behavior of instanceof

A common pitfall of using instanceof is believing that, if x instanceof C, then x was created using C as constructor. This is not true, because x could be directly assigned with C.prototype as its prototype. In this case

Overriding the behavior of instanceof

A common pitfall of using instanceof is believing that, if x instanceof C, then x was created using C as constructor. This is not true, because x could be directly assigned with C.prototype as its prototype. In this case

Object.prototype.isPrototypeOf

Learn how to contribute

This page was last modified on Jul 8, 2025 by MDN contributors.

Expressions and operators

Numbers and strings

Representing dates & times

Regular expressions

Indexed collections