This article will demonstrate the concept of checking whether an object is a class instance or not in JavaScript.
How to Check/Verify if an Object is Not an instanceof Class in JavaScript?
To check whether an object is an instance of the class or not in JavaScript, apply the following approaches in combination with the “instanceof” operator:
- “Logical Not(!)” operator.
- “Boolean Value”.
Let’s illustrate each of the approaches one by one!
Approach 1: Check/Verify if an Object is Not an instanceof Class in JavaScript Using the Logical Not(!) Operator
The “instanceof” operator in JavaScript is used to verify the object’s type at runtime. The “logical” operators are used to analyze the logic between values. More specifically, the logical “not(!)” operator gives the value “true” if a false value is indicated. These approaches can be utilized in combination to check the type of the created object with respect to a particular class.
Syntax
In the above syntax:
- “name” points to the object’s name.
- “type” corresponds to the object’s type.
Example
Let’s overview the below-stated example:
class Car {}
class Bike {}
let instClass = new Bike();
if (!(instClass instanceof Car)) {
console.log("Object is not an instance of class Car");
}
else {
console.log("Object is an instance of class Car");
}
</script>
In the above lines of code:
- Create two classes named “Car” and “Bike”, respectively.
- In the next step, create an object named “instClass” with the help of the “new” keyword and the “Bike()” constructor, respectively referring to the class “Bike”.
- Now, apply the logical “not(!)” operator along with the “instanceof” operator to check for the object’s instance with respect to the stated class.
- Upon the satisfied condition, the “if” condition will execute.
- In the other scenario, the “else” statement will be displayed.
Output
As evident from the output, the created object is the instance of the class “Bike” and not the “Car”.
Approach 2: Check if an Object is Not an instanceof Class in JavaScript Using Boolean Value
The values “true” and “false” represent the boolean values. These values can be utilized to apply a check upon the object with respect to the class based on a boolean value and display the corresponding result.
Example
The below-given example illustrates the stated concept:
class college{}
class university{}
let instClass = new college();
if (instClass instanceof university == false){
console.log("Object is not an instance of class university")
}
else {
console.log("Object is an instance of class Car");
}
</script>
In the above code snippet:
- Likewise, create two classes named “college” and “university”, respectively.
- After that, similarly, create an object of the class “college” named “instClass”.
- Now, apply the “instanceof” operator to check for the object’s instance with the help of the allocated boolean value “false”.
- Upon the satisfied condition, the former statement will be displayed.
- Otherwise, the latter statement in the “else” condition will execute.
Output
The above output indicates that the desired requirement is fulfilled.
Conclusion
The “instanceof” operator combined with the “Logical Not(!)” operator or the “Boolean Value” can be used to verify if an object is not an instance of the class in JavaScript. These approaches can be applied to create an object referring to one of the classes and check its instance. After that, the corresponding result with respect to the logical not(!) operator or the boolean value, respectively, is returned. This blog is guided to verify whether an object is an instance of the class or not in JavaScript.