- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript instanceof
JavaScript Classes
JavaScript instanceof
instanceofchecks whether an object was built from a particular class.
typeof only ever says object, which is not much help.
instanceof answers the more useful question.
Example
Example
javascript
class Dog {}
const rex = new Dog();
console.log(rex instanceof Dog);
console.log(typeof rex);The output is true then object.
What instanceof Does
It follows the prototype chain looking for the class you named.
That means it is true for parent classes as well.
It returns a plain boolean.
Syntax
Syntax
javascript
object instanceof ClassThe class goes on the right.
It Follows Inheritance
A child is an instance of its parent too.
Example
Example
javascript
class Animal {}
class Dog extends Animal {}
const rex = new Dog();
console.log(rex instanceof Dog);
console.log(rex instanceof Animal);Both are true, because Dog extends Animal.
It Is Not Symmetric
A parent instance is not an instance of the child.
Example
Example
javascript
class Animal {}
class Dog extends Animal {}
console.log(new Animal() instanceof Dog);The output is false.
Built-in Types
Arrays, dates and errors are all objects built from classes.
Example
Example
javascript
console.log([] instanceof Array);
console.log(new Date() instanceof Date);
console.log(new Error("x") instanceof Error);Array.isArray is still the safer check for arrays.
Checking the Exact Class
constructor.name tells you the exact class, ignoring inheritance.
Use it when a parent match would be wrong.
Example
Example
javascript
class Animal {}
class Dog extends Animal {}
const rex = new Dog();
console.log(rex.constructor.name);
console.log(rex.constructor === Animal);The output is Dog then false.
Prefer Checking Behaviour
Asking whether an object can do something is often better than asking what it is.
That keeps the code working with anything of the right shape.
Example
Example
javascript
class Dog {
speak() {
return "woof";
}
}
const thing = new Dog();
console.log(typeof thing.speak === "function" ? thing.speak() : "cannot speak");Any object with a speak method would work here.
Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript instanceof</title>
</head>
<body>
<h1>instanceof</h1>
<p id="out"></p>
<script>
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
const things = [new Dog(), new Cat(), new Animal()];
const lines = things.map(function (thing) {
return thing.constructor.name +
": Dog? " + (thing instanceof Dog) +
", Animal? " + (thing instanceof Animal);
});
document.getElementById("out").innerHTML = lines.join("<br>");
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try a plain object:
Add {} to the list and see both checks come back false.
Important Points
instanceofchecks the prototype chain.- It is true for parent classes as well.
- It is not true in the other direction.
constructor.namegives the exact class.- Checking for a method is often more flexible than checking the type.
Conclusion
instanceof answers the question typeof cannot.
Following inheritance is usually what you want, but know that it does.
For flexible code, checking what an object can do beats checking what it is.
