Reputation: 1244
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
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