288

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

514

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@hrishikeshp19 - I'm pretty sure you need the parens, I just tried that in chrome, IE and node and each host needed them.
@riship89 parens are required, proof: !! obj instanceof Array returns false (incorrect) while !!(obj instanceof Array) returns true (correct)
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.
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?
94
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.

4 Comments

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.
Only answers that explain the why of a problem (like this one) should get accepted...
@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.
Thanks! Makes one wonder why JavaScript has no such operator as !instanceof -- would it be good if the ecmascript folks make this awesome answer obsolete one day?
80

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 Comment

It actually looks cleaner than negation for me.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.