264

This is a really basic question really just to satisfy my curiosity, but is there a way to do something like this:

if(obj !instanceof Array) {
    //The object is not an instance of Array
} else {
    //The object is an instance of Array
}

The key here being able to use the NOT ! in front of instance. Usually the way I have to set this up is like this:

if(obj instanceof Array) {
    //Do nothing here
} else {
    //The object is not an instance of Array
    //Perform actions!
}

And its a little annoying to have to create an else statement when I simply want to know if the object is a specific type.

3 Answers 3

486

Enclose in parentheses and negate on the outside.

if(!(obj instanceof Array)) {
    //...
}

In this case, the order of precedence is important. See: Operator Precedence.

The ! operator precedes the instanceof operator.

4
  • 9
    @hrishikeshp19 - I'm pretty sure you need the parens, I just tried that in chrome, IE and node and each host needed them. Nov 16, 2012 at 5:38
  • 1
    @riship89 parens are required, proof: !! obj instanceof Array returns false (incorrect) while !!(obj instanceof Array) returns true (correct)
    – zamnuts
    Oct 20, 2013 at 23:47
  • 10
    The reason being is that !obj is evaluated first in if(!obj instanceof Array), which evaluates to true (or false), which then becomes if(bool instanceof Array), which is obviously false. Therefore, wrap it in parenthesis as suggested.
    – ronnbot
    Nov 26, 2013 at 20:07
  • 2
    This reason should really be part of the answer, otherwise this answer is not any better than the one of Chris below. @SergioTulentsev, would you be so kind and add something like this: In this case, the order of precedence is important (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Operator_Precedence). The ! operator precedes the instanceof operator. to your answer? Jan 18, 2019 at 15:26
89
if (!(obj instanceof Array)) {
    // do something
}

Is the correct way to check for this - as others have already answered. The other two tactics which have been suggested will not work and should be understood...

In the case of the ! operator without brackets.

if (!obj instanceof Array) {
    // do something
}

In this case, the order of precedence is important (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Operator_Precedence). The ! operator precedes the instanceof operator. So, !obj evaluated to false first (it is equivalent to ! Boolean(obj)); then you are testing whether false instanceof Array, which is obviously negative.

In the case of the ! operator before the instanceof operator.

if (obj !instanceof Array) {
    // do something
}

This is a syntax error. Operators such as != are a single operator, as opposed to a NOT applied to an EQUALS. There is no such operator as !instanceof in the same way as there is no !< operator.

3
  • NB. I would have made this a comment on Sergio's answer as that is obviously correct but I wasn't a member of SO so didn't have enough reputation points to comment. Mar 12, 2013 at 11:06
  • 5
    Only answers that explain the why of a problem (like this one) should get accepted... Feb 5, 2015 at 8:32
  • 1
    @chrismichaelscott In my opinion, and I am sure I am not alone, an answer like yours is what's most wanted by anyone asking a question here. It is clear, to the point, and shares enough information and examples to go around the issue presented. Thanks a lot. And I think you deserve the reputation and should have been the accepted answer.
    – cram2208
    Jul 25, 2016 at 1:50
71

As explained in the other answers negation doesn't work because:

"the order of precedence is important"

But it is easy to forget the double parenthesis so you can make a habit of doing:

if(obj instanceof Array === false) {
    //The object is not an instance of Array
}

or

if(false === obj instanceof Array) {
    //The object is not an instance of Array
}

Try it here

1
  • 14
    It actually looks cleaner than negation for me. Oct 4, 2016 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.