nkint
nkint

Reputation: 11733

check if two objects are both undefined or null in javascript

I have a method hitTest that check for collision detection and can return a Point object (if a collision is happened) or (if there is no collision) it returns null or undefined (i haven't deeply understand when it return null or undefined but i trust chrome console).

I have to test collision on 2 objects. And check if one or the two collisions are happening. I have tried this code:

var result1 = hitTest(player, object1);
var result2 = hitTest(player, object2);
if( result1 || result2 )  { blabla() };

but it doesn't work.

now.. i know that js is reallly a tricky language and i think about a smart way to do this without writing typeof 4 times. I'm thinking about python short-circuit logical operators...

Upvotes: 1

Views: 1169

Answers (3)

Onur Yıldırım
Onur Yıldırım

Reputation: 33644

You wouldn't need to write typeof 4 times already but anyway;

Coercion paradigm for conditional statements and operators:

//TYPE           //RESULT
Undefined        // false
Null             // false
Boolean          // The result equals the input argument (no conversion).
Number           // The result is false if the argument is +0, −0, or NaN; otherwise the result is true.
String           // The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
Object           // true

From Mozilla:

Logical AND (&&)

expr1 && expr2
If the first operand (expr1) can be converted to false, the && operator returns false rather than the value of expr1.

Logical OR (||)

expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false.

true || false // returns true
true || true // returns true
false || true // returns true
false || false // returns false
"Cat" || "Dog"     // returns Cat
false || "Cat"     // returns Cat
"Cat" || false     // returns Cat

true && false // returns false
true && true // returns true
false && true // returns false
false && false // returns false
"Cat" && "Dog" // returns Dog
false && "Cat" // returns false
"Cat" && false // returns false

Additionally, you can use a shortcut isset() method just like in PHP to properly validate your objects:

function isSet(value) {
    return typeof(value) !== 'undefined' && value != null;
}

So; your code would be:

var result1 = hitTest(player, object1),
    result2 = hitTest(player, object2);
if ( isSet(result1) && isSet(result2) )  { blabla(); };

Upvotes: 1

Teemu
Teemu

Reputation: 23396

You can use &&, it returns the first detected false/null/undefined/0, i.e. if won't pass, if either result1 or result2 is null.

Upvotes: 4

ilan berci
ilan berci

Reputation: 3881

for this type of thing, underscore.js is beautifull: http://underscorejs.org/#isNull and http://underscorejs.org/#isUndefined

I use these helpers frequently to get around edge cases in JS such as the ones you mentioned

Upvotes: 1

Related Questions