Export (0) Print
Expand All
1 out of 3 rated this helpful - Rate this topic

instanceof Operator (JavaScript)

Returns a Boolean value that indicates whether or not an object is an instance of a particular class.

result = object instanceof class
result

Required. Any variable.

object

Required. Any object expression.

class

Required. Any defined object class.

The instanceof operator returns true if object is an instance of class. It returns true if true if class is present in the object's prototype chain. It returns false if object is not an instance of class, or if object is null.

The following example shows how to use the instanceof operator.

function objTest(obj){
    var i, t, s = "";
    t = new Array();
    t["Date"] = Date;
    t["Object"] = Object;
    t["Array"] = Array;
        for (i in t){
            if (obj instanceof t[i]) { 
                s += "obj is an instance of " + i + "<br/>";
            }
            else {
                s += "obj is not an instance of " + i + "<br/>";
        }
    }
    return(s);
}

var obj = new Date();
document.write(objTest(obj));

// Output: 
// obj is an instance of Date
// obj is an instance of Object
// obj is not an instance of Array

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.