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…
!!forces the result to be a boolean.If you pass
null, for example, then the&&will returnnull. The!!converts that tofalse.If
objis “truthy”, you’ll get the result ofobj.nodeType == 1which is a boolean.