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.
The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. Its syntax is. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .
Strictly speaking it is a function object, since everything is an object in Javascript. And this function object has a prototype, because every function has a prototype. The instanceof operator should be avoided because it fakes classes, which do not exist in Javascript.
The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.
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
.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With