Primitives are a different kind of type than objects created from within Javascript. From the Mozilla API docs:

var color1 = new String("green");
color1 instanceof String; // returns true
var color2 = "coral";
color2 instanceof String; // returns false (color2 is not a String object)

I can't find any way to construct primitive types with code, perhaps it's not possible. This is probably why people use typeof "foo" === "string" instead of instanceof.

An easy way to remember things like this is asking yourself "I wonder what would be sane and easy to learn"? Whatever the answer is, Javascript does the other thing.

Answer from John Millikin on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › instanceof
instanceof - JavaScript | MDN
July 8, 2025 - 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.hasInstance.
🌐
W3Schools
w3schools.com › jsref › jsref_oper_instanceof.asp
JavaScript instanceof Operator
instanceof is an ECMAScript3 (JavaScript 1999) feature. It is supported in all browsers: The spread (...) Operator · The in Operator · The typeof Operator · The delete Operator · The void Operator · ❮ Previous Miscellaneous Operators Next ❯ · ★ +1 · Sign in to track progress ·
Discussions

javascript - Why does instanceof return false for some literals? - Stack Overflow
I can't find any way to construct ... use typeof "foo" === "string" instead of instanceof. An easy way to remember things like this is asking yourself "I wonder what would be sane and easy to learn"? Whatever the answer is, Javascript does the other thing.... More on stackoverflow.com
🌐 stackoverflow.com
What is the instanceof operator in JavaScript? - Stack Overflow
The instanceof keyword in JavaScript can be quite confusing when it is first encountered, as people tend to think that JavaScript is not an object-oriented programming language. What is it? What pr... More on stackoverflow.com
🌐 stackoverflow.com
instanceof, why not?

instanceof is usually code smell. As in, there is probably a better way to achieve the same thing.

For numbers and strings, typeof is a good option. For functions, duck typing is often a better choice than instanceof. So your code might look like this:

if (typeof obj.dispose === 'function')
    obj.dispose() 

constructor is unreliable and can be overridden easily. You should never rely on it to be a particular value.

More on reddit.com
🌐 r/javascript
16
8
September 26, 2014
How does the "instanceof" operator work?
Tell us what’s happening: How does theinstanceof operator infer that a parent object, is a child object’s constructor, without explicitly setting the constructor property inside parent.prototype? I understand that by overwriting Dog.prototype with a new object (to improve inheritance ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
February 5, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › instanceof-operator-in-javascript
JavaScript Instanceof Operator - GeeksforGeeks
July 11, 2025 - The instanceof operator in JavaScript is used to check the type of an object at run time.
🌐
Medium
medium.com › @AlexanderObregon › the-mechanics-behind-javascripts-typeof-and-instanceof-3299ff38f20a
The Mechanics of JavaScript’s typeof and instanceof | Medium
May 16, 2025 - The item still points to the old prototype, so instanceof returns false. Again, the actual constructor is irrelevant, it’s the current state of the prototype link that decides the result. JavaScript even lets you customize how instanceof works using Symbol.hasInstance.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › classes
Class checking: "instanceof"
September 26, 2025 - The instanceof operator allows to check whether an object belongs to a certain class.
Find elsewhere
🌐
CoreUI
coreui.io › blog › what-is-the-difference-between-typeof-and-instanceof-in-javascript
What is the difference between typeof and instanceof in JavaScript · CoreUI
September 17, 2024 - The instanceof operator checks if an object is an instance of a specific class or constructor function.
Top answer
1 of 11
318

instanceof

The Left Hand Side (LHS) operand is the actual object being tested to the Right Hand Side (RHS) operand which is the actual constructor of a class. The basic definition is:

Checks the current object and returns true if the object is of the specified object type.

Here are some good examples and here is an example taken directly from Mozilla's developer site:

var color1 = new String("green");
color1 instanceof String; // returns true
var color2 = "coral"; //no type specified
color2 instanceof String; // returns false (color2 is not a String object)

One thing worth mentioning is instanceof evaluates to true if the object inherits from the class's prototype:

var p = new Person("Jon");
p instanceof Person

That is p instanceof Person is true since p inherits from Person.prototype.

Per the OP's request

I've added a small example with some sample code and an explanation.

When you declare a variable you give it a specific type.

For instance:

int i;
float f;
Customer c;

The above show you some variables, namely i, f, and c. The types are integer, float and a user defined Customer data type. Types such as the above could be for any language, not just JavaScript. However, with JavaScript when you declare a variable you don't explicitly define a type, var x, x could be a number / string / a user defined data type. So what instanceof does is it checks the object to see if it is of the type specified so from above taking the Customer object we could do:

var c = new Customer();
c instanceof Customer; //Returns true as c is just a customer
c instanceof String; //Returns false as c is not a string, it's a customer silly!

Above we've seen that c was declared with the type Customer. We've new'd it and checked whether it is of type Customer or not. Sure is, it returns true. Then still using the Customer object we check if it is a String. Nope, definitely not a String we newed a Customer object not a String object. In this case, it returns false.

It really is that simple!

2 of 11
97

There's an important facet to instanceof that does not seem to be covered in any of the comments thus far: inheritance. A variable being evaluated by use of instanceof could return true for multiple "types" due to prototypal inheritance.

For example, let's define a type and a subtype:

function Foo(){ //a Foo constructor
    //assign some props
    return this;
}

function SubFoo(){ //a SubFoo constructor
    Foo.call( this ); //inherit static props
    //assign some new props
    return this;
}

SubFoo.prototype = Object.create(Foo.prototype); // Inherit prototype
SubFoo.prototype.constructor = SubFoo;

Now that we have a couple of "classes" lets make some instances, and find out what they're instances of:

var 
    foo = new Foo()
,   subfoo = new SubFoo()
;

alert( 
    "Q: Is foo an instance of Foo? "
+   "A: " + ( foo instanceof Foo ) 
); // -> true

alert( 
    "Q: Is foo an instance of SubFoo? " 
+   "A: " + ( foo instanceof SubFoo ) 
); // -> false

alert( 
    "Q: Is subfoo an instance of Foo? "
+   "A: " + ( subfoo instanceof Foo ) 
); // -> true

alert( 
    "Q: Is subfoo an instance of SubFoo? "
+   "A: " + ( subfoo instanceof SubFoo ) 
); // -> true

alert( 
    "Q: Is subfoo an instance of Object? "
+   "A: " + ( subfoo instanceof Object ) 
); // -> true

See that last line? All "new" calls to a function return an object that inherits from Object. This holds true even when using object creation shorthand:

alert( 
    "Q: Is {} an instance of Object? "
+   "A: " + ( {} instanceof Object ) 
); // -> true

And what about the "class" definitions themselves? What are they instances of?

alert( 
    "Q: Is Foo an instance of Object? "
+   "A:" + ( Foo instanceof Object) 
); // -> true

alert( 
    "Q: Is Foo an instance of Function? "
+   "A:" + ( Foo instanceof Function) 
); // -> true

I feel that understanding that any object can be an instance of MULTIPLE types is important, since you may (incorrectly) assume that you could differentiate between, say an object and a function by use of instanceof. As this last example clearly shows a function is an object.

This is also important if you are using any inheritance patterns and want to confirm the progeny of an object by methods other than duck-typing.

🌐
Reddit
reddit.com › r/javascript › instanceof, why not?
r/javascript on Reddit: instanceof, why not?
September 26, 2014 -

Good night reddit,

I have been developing Javascript for over 2 years now and just recently learn the advantages of using 'instanceof'.

Since then, I find myself using "(function(){}) instanceof Function" and not "typeof" because I don't like string comparison at such low level.

And it seems really reliable except for primitives types...

For which i use (5).constructor === Number or ("").constructor === String.

I guess I really hate typeof :P

I know this probably won't give any performance boost, but is there a reason for not using it? Maybe some sort IE bug from 2003? I found only a few articles that did not advise against it, but the lack of knowledge of it irks me.

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How does the "instanceof" operator work? - JavaScript - The freeCodeCamp Forum
February 5, 2023 - Tell us what’s happening: How does theinstanceof operator infer that a parent object, is a child object’s constructor, without explicitly setting the constructor property inside parent.prototype? I understand that by overwriting Dog.prototype with a new object (to improve inheritance ...
🌐
John Kavanagh
johnkavanagh.co.uk › home › articles › instanceof: understanding javascript object types
instanceof: Understanding JavaScript Object Types, by John Kavanagh
June 1, 2024 - Starting with the basics instanceof is a binary operator used in JavaScript to check whether an object is an instance of a particular class or constructor function.
Price   $$
Address   66 Paul Street, EC2A 4NE, London
🌐
Reddit
reddit.com › r/typescript › if typescript is duck-typed, how come when you use "instanceof", typescript accurately separate out the individual classes that implement the same interface? and how can we take advantage of this functionality in our typing?
r/typescript on Reddit: If typescript is duck-typed, how come when you use "instanceof", typescript accurately separate out the individual classes that implement the same interface? And how can we take advantage of this functionality in our typing?
October 23, 2024 -

Edit: I know that instanceof checks at runtime, I'm very familiar with it and have been using it for many years. My question is specifically how the typing system works in typescript, how/why typescript is able to discern via type inference the difference between Error and PseudoError. And, given that it can, whether it is possible to leverage that kind of discernment to create a utility function that's based on this rather than based on shapes and interfaces.

In lib.es5.d.ts in typescript we have:

interface Error {
    name: string;
    message: string;
    stack?: string;
}

Now in my typescript file, if I create a new type, like this:

class PseudoError {
    name = "hello"
    message = "world"
    stack? = "friends"
}

And I do the following:

const test : PseudoError | Error = new PseudoError()

type withoutError = Exclude<typeof test, Error> // <--- results to never, because of duck typing ?

The above is expected, because PseudoError and Error implement the same interface. So if I'm excluding Error, then I'm also excluding PseudoError by duck type standards.

But when I do this:

if (test instanceof Error) {
  test // <--- hover over = Error
} else {
  test // <--- hover over = PseudoError
}

It suggests that there is obviously some built-in functionality in typescript that doesn't work like in a duck-type-y way. This clearly aligns with the desired outcome in JS, but in my situation, I would like to have an Exclude<...> utility type that does the same thing as what typescript is doing behind the scenes with instanceof.

For example, where's this NotInstanceOf utility function?

const test : PseudoError | Error = new PseudoError()

type withoutError = NotInstanceOf<typeof test, Error> // <--- results to PsuedoError
🌐
W3Schools
w3schools.com › js › tryit.asp
JavaScript Operators
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Errors › invalid_right_hand_side_instanceof_operand
TypeError: invalid 'instanceof' operand 'x' - JavaScript | MDN
July 8, 2025 - The instanceof operator expects the right-hand-side operands to be a constructor object, i.e., an object which has a prototype property and is callable. It can also be an object with a Symbol.hasInstance method.
🌐
Flexiple
flexiple.com › javascript › instanceof-javascript
JavaScript instanceof operator: Syntax, Example & Explanation - Flexiple
March 11, 2022 - In this article, we shall look at the instanceof operator and understand what it actually does with examples. The JavaScript instanceof operator is used to check the type of an object at the run time. It returns a boolean value(true or false).
🌐
JavaScript in Plain English
javascript.plainenglish.io › what-is-the-instanceof-operator-in-javascript-2f74e88a2a4d
What Is the instanceof Operator in JavaScript | by Codecupdev | JavaScript in Plain English
October 21, 2022 - In JavaScript instanceof is an operator. An operator is either used for calculations or for logic. The instanceof operator will see if an operand (what we perform the operation on) is an instance of the value we pass.
🌐
Medium
medium.com › @codezone › the-instanceof-operator-in-object-oriented-javascript-f0095e17d41d
The instanceof Operator in Object-Oriented JavaScript | by codezone | Medium
August 20, 2023 - The instanceof operator is a valuable tool in object-oriented JavaScript that allows you to determine if an object is an instance of a particular constructor or class. This concept is pivotal in understanding object relationships, inheritance, ...
🌐
GitHub
github.com › mdn › content › blob › main › files › en-us › web › javascript › reference › operators › instanceof › index.md
content/files/en-us/web/javascript/reference/operators/instanceof/index.md at main · mdn/content
For [bound functions](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind), `instanceof` looks up for the `prototype` property on the target function, since bound functions don't have `prototype`.
Author   mdn
🌐
Mostafa-samir
mostafa-samir.github.io › classingjs › tutorial › instanceof.html
Tutorial : Object::instanceOf
It's extremly simple to use , just call yourObject.instanceOf(ancestor) and it will return true if your object is an instance of that ancestor, or false otherwise.