262

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.

ryandlf
  • 27,155
  • 37
  • 106
  • 162

3 Answers3

482

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.

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 9
    @hrishikeshp19 - I'm pretty sure you need the parens, I just tried that in chrome, IE and node and each host needed them. – Marcus Pope Nov 16 '12 at 05:38
  • 1
    @riship89 parens are required, proof: ``!! obj instanceof Array`` returns false (incorrect) while ``!!(obj instanceof Array)`` returns true (correct) – zamnuts Oct 20 '13 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 '13 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? – j3141592653589793238 Jan 18 '19 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.

  • 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. – chrismichaelscott Mar 12 '13 at 11:06
  • 5
    Only answers that explain the **why** of a problem (like this one) should get accepted... – Robert Rossmann Feb 05 '15 at 08: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 '16 at 01:50
69

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

Wilt
  • 41,477
  • 12
  • 152
  • 203