Last updated: Mar 2, 2024
Reading time·4 min
If you need to check if an object is
NOT
an instance of a class, click on the second subheading.
Use the instanceof
operator to check if an object is an instance of a
class.
The instanceof
operator will return true
if the object is an instance of
the class and false
otherwise.
class Person {} const p1 = new Person(); if (p1 instanceof Person) { console.log('✅ is instance of Person'); } else { console.log('⛔️ is not instance of Person'); } class Animal {} console.log(p1 instanceof Animal); // 👉️ false
The instanceof operator returns a boolean value indicating whether the prototype property of the constructor appears in the prototype chain of the object.
The p1
object was created using the Person
class, so it is an instance of
the class.
Here is an example that uses the instanceof
operator to determine which method
is available on an object.
class Person { walk() { console.log('person is walking'); } } class Animal { run() { console.log('animal is running'); } } function example(x) { // 👉️ x is type Person or Animal here if (x instanceof Person) { // 👇️ x is type Person here x.walk(); } else { // 👇️ x is type Animal here x.run(); } } const p1 = new Person(); const a1 = new Animal(); example(p1); // 👉️ "person is walking" example(a1); // 👉️ "animal is running"
The function takes a parameter of type Person
or Animal
, so before we access
a class-specific method, we have to check an instance of which class was passed
to the function.
If you access the constructor.name
property, you can see that the class name
of the p1
object is Person
.
class Person {} const p1 = new Person(); console.log(p1.constructor.name); // 👉️ Person
We accessed the name
property on the
Object.constructor
property.
The Object.constructor
property returns a reference to the constructor
function, from which the object was created.
The instanceof
operator checks for the presence of constructor.prototype
in
the object's prototype chain.
class Person {} const p1 = new Person(); // 👇️ true console.log(Object.getPrototypeOf(p1) === Person.prototype);
Alternatively, you can use the logical NOT (!) operator.
The logical NOT (!) operator inverts the value the instanceof
operator
returns.
class Person {} class Animal {} const p1 = new Person(); if (!(p1 instanceof Animal)) { console.log('✅ Object is not instance of Animal'); } else { console.log('⛔️ Object is instance of Animal'); }
The instanceof
operator returns a boolean value indicating if the prototype
property of the constructor appears in the prototype chain of the object.
Notice that we used parentheses around the instanceof check before negating it.
A commonly seen mistake is to omit the parentheses, e.g.:
// ❌ Don't do this console.log(!p1 instanceof Animal); // 👉️ false
The code sample inverts the value of the object and converts it to a boolean, so
it becomes false
.
class Person {} const p1 = new Person(); console.log(!p1); // 👉️ false
We then check if false
is an instance of the Animal
class which will always
return false
.
class Person {} class Animal {} const p1 = new Person(); if (!(p1 instanceof Animal)) { console.log('✅ Object is not instance of Animal'); } else { console.log('⛔️ Object is instance of Animal'); }
The p1 instanceof Animal
expression is first evaluated and returns false
if
the object is not an instance of the class.
Then the logical NOT (!) operator inverts the value to true
and the if
block
runs.
If the object is an instance of the class, the else
block runs.
If you don't like the parentheses approach, compare the value to the boolean
false
.
This is a three-step process:
instanceof
operator to check if the object is an instance of the
class.false
.instanceof
operator returned false
, the object is not an instance
of the class.class Person {} class Animal {} const p1 = new Person(); if (p1 instanceof Animal === false) { console.log('✅ Object is not instance of Animal'); } else { console.log('⛔️ Object is instance of Animal'); }
We first used the instanceof
operator to test if the p1
object is an
instance of the Animal
class.
If the test returns false
, then the object is not an instance of the class.
if
block runs, otherwise, the else
block runs.This approach achieves the same goal as using the logical NOT (!) operator with parentheses.
However, it might be a bit more readable because you don't have to think about the order in which the statements are run.
Here are some more examples of using the logical NOT (!) operator.
console.log(!true); // 👉️ false console.log(!false); // 👉️ true console.log(!'str'); // 👉️ false console.log(!''); // 👉️ true console.log(!null); // 👉️ true
The logical NOT (!) operator converts truthy values to false
and fasly values
to true
.
The falsy values in JavaScript are: false
, 0
, -0
, ""
(empty string),
null
, undefined
, NaN
(Not a number).
When used with any other than the aforementioned 6 values, the logical NOT (!)
operator returns false
.
If you decide to use the logical NOT (!) operator approach, make sure to wrap
the instanceof
test in parentheses.
class Person {} class Animal {} const p1 = new Person(); if (!(p1 instanceof Animal)) { console.log('✅ Object is not instance of Animal'); } else { console.log('⛔️ Object is instance of Animal'); }