Reputation: 10310
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 uses a triple exclamation mark syntax. I've done some testing (and I'm pretty sure I missed something), and the result is always the same a single !
. I assumed it was somewhat equivalent to ===
and !==
...
Can anyone explain what the !!!
syntax means?
Upvotes: 2
Views: 2216
Reputation: 489
!!! is a triple negation, so it is the same as !:
!true -> false
!!true -> true
!!!true -> false
Upvotes: 0
Reputation: 3687
This answers your question perfectly https://stackoverflow.com/a/264037/1561922
!!!x is probably inversing a boolean conversion !!x:
var myBool = Boolean("false"); // == true
var 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:
function ok(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.
Upvotes: -1
Reputation: 239311
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.
Upvotes: 6