What Is the Instanceof Operator in JavaScript

What is the instanceof operator in JavaScript?

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!

What is the difference between typeof and instanceof and when should one be used vs. the other?

Use instanceof for custom types:

var ClassFirst = function () {};
var ClassSecond = function () {};
var instance = new ClassFirst();
typeof instance; // object
typeof instance == 'ClassFirst'; // false
instance instanceof Object; // true
instance instanceof ClassFirst; // true
instance instanceof ClassSecond; // false

Use typeof for simple built in types:

'example string' instanceof String; // false
typeof 'example string' == 'string'; // true

'example string' instanceof Object; // false
typeof 'example string' == 'object'; // false

true instanceof Boolean; // false
typeof true == 'boolean'; // true

99.99 instanceof Number; // false
typeof 99.99 == 'number'; // true

function() {} instanceof Function; // true
typeof function() {} == 'function'; // true

Use instanceof for complex built in types:

/regularexpression/ instanceof RegExp; // true
typeof /regularexpression/; // object

[] instanceof Array; // true
typeof []; //object

{} instanceof Object; // true
typeof {}; // object

And the last one is a little bit tricky:

typeof null; // object

Understanding of instanceof operator

instanceof operator tests only object. You try to test primitives, so it returns false.
More here

How does instanceof operator actually work in JS?

JavaScript has a feature called autoboxing which converts primitives to Objects when you try to access properties on them.

instanceof doesn't autobox and a primitive number is not an instance of the Number class.

You can see, in the specification:


  1. If Type(O) is not Object, return false.

Javascript: Still confused by the instanceof operator

The exact wording is important. You talk about the constructor being in the prototype chain, but the original quote doesn't:

The instanceof operator tests whether an object has in its prototype
chain the prototype property of a constructor.

So the expression rt instanceof RTextCell actually is testing something like this (keeping in mind that __proto__ is not standard):

var p = rt.__proto__;
while(p)
{
if(p == RTextCell.prototype)
return true;
p = p.__proto__;
}
return false;

So even though the function RTextCell is not referenced directly in the object trees above, the RTextCell.prototype object is.

Why does JavaScript's instanceof operator say that an object is not an Object?

It may be from a different window (e.g. an iframe), which has an own Object constructor

var obj = frame.contentWindow.obj;
console.log(obj instanceof Object); // false
console.log(obj instanceof frame.contentWindow.Object); // true

http://jsfiddle.net/JnfPR/1/

Also note that there is a variety of objects in JS, including "Array objects", "RegExp objects", "Function objects", ... and "Object objects". The typeof operator is not very helpful there. It can only distinguish between Function objects and other objects.



Related Topics



Leave a reply



Submit