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?

Sorskoot
  • 10,190
  • 6
  • 55
  • 98

3 Answers3

5

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • I can't believe it's just that... What would be the use of writing it with 3 ! instead of just 1? – Sorskoot Oct 23 '13 at 19:10
  • @Sorskoot: There is no use. None whatsoever :-D – gen_Eric Oct 23 '13 at 19:10
  • 4
    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. – user229044 Oct 23 '13 at 19:10
  • 2
    It's the coder's equivalent of ["More Cowbell!"](http://en.wikipedia.org/wiki/More_cowbell) – Michael Berkowski Oct 23 '13 at 19:20
  • "an arbitrary number.." only *odd* numbers, as an even number will invert the meaning. (So !!!! is the opposite of !!! -- whoo!!!!) – Jongware Oct 23 '13 at 19:58
  • 1
    @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. – user229044 Oct 23 '13 at 20:01
  • @meagar: what I meant is, whether you start out with either a single `!` or double `!!`, the (useless) extension is `!!` -- repeated *ad nauseum* -- so the total number *stays* odd or even. – Jongware Oct 23 '13 at 20:06
  • Er no, they were probably trying to use inverse on a boolean conversion. !!x turns x into a boolean no matter what x's data type. If x is not null, it is true. So !!!x basically makes the opposite of !!x. – geoyws May 19 '14 at 15:19
  • 1
    @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`. – user229044 May 19 '14 at 17:39
  • You're right. Node.js just fixed their assert.js in 0.11. https://github.com/joyent/node/commit/dc9acd4faeba1aade414bdd8da28f30b16773575 – geoyws May 20 '14 at 00:22
  • I feel this answer is wrong and the answer below by @geoyws is the right one. We are talking in term of JS here and the "!!!" is "! (!!)" which is to check for negation (false) of some variable. – Sarang May 06 '20 at 08:54
  • @sarang The answer below is wrong. You don't need to negate a third time, that is identical to negating a single time. – user229044 May 06 '20 at 12:10
0

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

!true -> false

!!true -> true

!!!true -> false
LSerni
  • 55,617
  • 10
  • 65
  • 107
Unstaged
  • 449
  • 2
  • 5
  • 14
-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.

Community
  • 1
  • 1
geoyws
  • 3,326
  • 3
  • 35
  • 47
  • 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](http://stackoverflow.com/a/23741550/464709) comes dangerously close to the abuse line. Please take some time to read the [Help Center](http://stackoverflow.com/help), and please refrain from posting such answers in the future. Thank you. – Frédéric Hamidi May 22 '14 at 18:36
  • @FrédéricHamidi It's still in use for the [current version (v0.10.xx)](http://nodejs.org/download/) downloaded from Nodejs.org. It is only implemented in v0.11.xx onwards which is only available via other channels. – geoyws May 23 '14 at 02:02
  • @FrédéricHamidi you clearly didn't read the links. Especially the last one. [This is the reason why.](http://stackoverflow.com/questions/23741106/why-does-node-js-assert-js-use-value-in-their-code-what-purpose-does-it-ser?noredirect=1#comment-36498421) 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. – geoyws May 23 '14 at 02:12
  • 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. – Frédéric Hamidi May 23 '14 at 07:31
  • This is the right and perfect answer to the main question IMHO. – Sarang May 06 '20 at 08:54
  • @sarang except it's wrong. It demonstrated no difference whatsoever between `!` and `!!!` because there are none. – user229044 May 06 '20 at 12:12
  • @meagar hmm.. now that I read more about it, I realize using "!!" is wrong then? I am not a JS pro, but I used to use "!!" to convert stuff to boolean, I guess I need to stop using that then? – Sarang May 15 '20 at 18:12
  • @sarang no, not at all. !! Is fine. This question is about !!! which is useless. – user229044 May 15 '20 at 23:33
  • ok, got it! So for negation use !(x) and for regular use !!(x). Is that correct? – Sarang May 17 '20 at 08:04