2

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?

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

12 Comments

I can't believe it's just that... What would be the use of writing it with 3 ! instead of just 1?
As I said, there is no use. There cannot be any valid use ever, except obfuscation. It is always equivalent to a single !. The programmer was being silly.
It's the coder's equivalent of "More Cowbell!"
@Jongware No, not only odd numbers; As I said, you can chain an arbitrary number together, and it isn't useful for anything. The only useful numbers are ! and !!, which I covered in my answer.
@Geoyws That's exactly what I said in my answer. !!!x is the negation of !!x, but it's useless, because it's equivalent to !x. There is no reason to ever use !!!x instead of !x.
|
0

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

!true -> false

!!true -> true

!!!true -> false

Comments

-1

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.

9 Comments

This question was asked (and answered) seven months ago. Your answer apparently does not bring much to the table, apart from mentioning code like this used to be in Node but now doesn't. That could have been achieved with a comment, possibly containing a link to your question. Posting such an answer once would be understandable, posting it more than once comes dangerously close to the abuse line. Please take some time to read the Help Center, and please refrain from posting such answers in the future. Thank you.
@FrédéricHamidi It's still in use for the current version (v0.10.xx) downloaded from Nodejs.org. It is only implemented in v0.11.xx onwards which is only available via other channels.
@FrédéricHamidi you clearly didn't read the links. Especially the last one. This is the reason why. which is a comment by Jonathan Lonowski. Please take an unrealistic amount of time to read and understand all things on hand before commenting. Thank you.
You may have missed my point. I have read these links, and although they probably are of interest to you, they're not really related to this question (the questioner here does not care about Node and only asked about the !!! construct). You also posted the exact same late answer under another question, which is a problem.
This is the right and perfect answer to the main question IMHO.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.