scusyxx
scusyxx

Reputation: 1244

Underscore.js _.isElement function

I have been looking underscore.js library functions and I noticed a function which returns whether the element is a DOM element or not. The function is below.

_.isElement = function(obj) {
    return !!(obj && obj.nodeType == 1);
};

Can you please tell me why !! is being used instead of just returning (obj && obj.nodeType == 1). I am wondering whether !! adds any performance improvements. Any idea...

Upvotes: 0

Views: 619

Answers (1)

gen_Eric
gen_Eric

Reputation: 227240

!! forces the result to be a boolean.

If you pass null, for example, then the && will return null. The !! converts that to false.

If obj is "truthy", you'll get the result of obj.nodeType == 1 which is a boolean.

Upvotes: 5

Related Questions