Skip to content Skip to sidebar Skip to footer

What Does The '!!!' Syntax Mean In Javascript?

I just came across the following line of code in one of the Windows Store Apps samples. if (that.assets[asset].object === null || !!!that.assets[asset].object.canPlayType) { It us

Solution 1:

I assumed it was somewhat equivalent to === and !==...

No, it's just three "not" operators, a "not-not-not".

It's the same as !(!(!(x))), and is always equivalent to a single !x.

There is literally no use for this. !! is a somewhat cryptic means of converting any variable to its boolean representation, but !!! is just silly. You can chain an arbitrary number of !'s together, but it isn't useful for anything.

Solution 2:

!!! is a triple negation, so it is the same as !:

!true->false

!!true->true

!!!true->false

Solution 3:

This answers your question perfectly https://stackoverflow.com/a/264037/1561922

!!!x is probably inversing a boolean conversion !!x:

var myBool = Boolean("false");  // == truevar myBool = !!"false";  // == true

"Any string which isn't empty will evaluate to true"

So !!!"false"; // == false

This question is NOT a joke. Node.js (downloaded 5 days ago) uses this in Assert.js for example:

functionok(value, message) {
  if (!!!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;

EDIT: I think they did it for code readability reasons out of habit, because !value already suffices.

EDIT: Node changed it. I have no idea why my version of Node.js that was downloaded 5 days ago is still with the !!!value instead of the !value in GitHub.

EDIT: Jonathan explains why here. Nodejs.org's stable version, v0.10.xx still has !!!value, and the unstable version v0.11.xx has the !value amendment.

Post a Comment for "What Does The '!!!' Syntax Mean In Javascript?"