ericTbear ericTbear - 14 days ago 5
Javascript Question

JavaScript: Is '!0' shorthand of Boolean 'true'?

Whilst updating Google Analytics from async version to universal, I came across this event tracking line, written by someone else years ago.

_gaq.push(["_trackEvent", "Social Widget Share", "Shared to Facebook", "Share to Facebook via the Social Driver Widget", 0, !0])


My understanding is that the last element is to indicate a Non-Interaction event with "true" otherwise it is left out of the array.

I have never seen !0 and I can not find any indication searching here or elsewhere on the web that !0 is a thing when it comes to Boolean notation - or analytics event tracking.

Answer

Yes, that's correct and please have a look at some of the true and false values below

// False values
console.log('False values');
console.log(Boolean(false));
console.log(Boolean(0));
console.log(Boolean(!!0));
console.log(Boolean(!1));
console.log(Boolean(''));
console.log(Boolean(null));
console.log(Boolean(undefined));

// True values
console.log('True values');
console.log(Boolean('0')); // string variable
console.log(Boolean(!0));
console.log(Boolean(1));
console.log(Boolean(!!1));
console.log(Boolean(true));