Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  160] [ 1]  / answers: 1 / hits: 94373  / 13 Years ago, mon, january 16, 2012, 12:00:00

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.


More From » instanceof

 Answers
137

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.


[#88002] Friday, January 13, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nestorjarettg

Total Points: 451
Total Questions: 108
Total Answers: 108

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;