简体   繁体   中英

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 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?

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.

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

!true -> false

!!true -> true

!!!true -> false

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 !!!"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM