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

!!!value basically means not not not Boolean(value) yes?

Say if value = 9 > 1, then it would mean: not not not true.

not true = false
not not true = true
not not not true = false

My brain hurts. Why don't they just use !value instead of !!!value?

geoyws
  • 3,326
  • 3
  • 35
  • 47
  • Presumably, it is because it is very hard to overlook when reading the code. – Quentin May 19 '14 at 15:03
  • 2
    Maybe because "they" started with with boolean conversion only (`!!`), then had to negate the condition later and lost track of the meaning of the code in-between? – Frédéric Hamidi May 19 '14 at 15:04
  • 1
    @MichaelBorgwardt rather http://stackoverflow.com/questions/19550294/what-does-the-syntax-mean-in-javascript – GôTô May 19 '14 at 15:12
  • @MichaelBorgwardt Haha, that duplicate question has a -1 vote! Awesomesauce. – geoyws May 19 '14 at 15:16
  • 3
    There's an issue on the project about this, [#5885](https://github.com/joyent/node/pull/5885), that's already been resolved, [dc9acd4](https://github.com/joyent/node/commit/dc9acd4faeba1aade414bdd8da28f30b16773575). – Jonathan Lonowski May 19 '14 at 15:24
  • 1
    @JonathanLonowski If it has been [resolved already in July 2013 by bnoordhuis](https://github.com/joyent/node/commit/dc9acd4)... why am I seeing it in my Node.js installation that I just downloaded and installed like 5 days ago? – geoyws May 19 '14 at 15:40
  • 1
    @Geoyws Different branches. It's been resolved for 0.11, but [remains unchanged for 0.10](https://github.com/joyent/node/blob/v0.10/lib/assert.js#L111-L113). – Jonathan Lonowski May 19 '14 at 15:52
  • 1
    @JonathanLonowski Wow man. Thanks, that's a sanity-saver. Could you write an answer please? Just so the community can benefit. – geoyws May 19 '14 at 15:56

4 Answers4

3

!value and !!!value both do have the same effect.

But, they intenionally use !! to convert the potential 'truthy' or 'falsy' variable value to a boolean.

So it does makes sense to use !!! when using value as a negated boolean, so developers can see that (in this case) value may only be 'truthy', e.g. a string and not true.

If in refactoring someone needs to set something to true and uses value, it may be set to a string. The !!! tells him to use !!value, if he expects value as a boolean.

jukempff
  • 965
  • 6
  • 12
  • R.E your second paragraph; it makes *more* sense to use a single `!`, as it has the *same* effect as `!!!`. – Matt May 19 '14 at 16:18
  • This utterly fails to address the question of why !!!value is different than !value, which it in fact isn't. – Sam Hanley May 19 '14 at 17:28
  • The Question wasnt *why !!!value is different than !value*. The question was why they did write it that way. – jukempff May 20 '14 at 10:41
  • @Matt yes it has the same *effect*. But the triple `!` states that `value` does not necessarily has to be `true`. – jukempff May 20 '14 at 10:44
  • @juk: I'm not sure what you mean. A single `!` does not require `value`` to be true either. – Matt May 20 '14 at 10:54
  • Well, I see the `!!!` as a simple warning to maintainers, that `value` could also be a `string`, `number`, or whatever and shall not to be expected to be a Boolean. Not specifically dangerous in asserts `ok` method, but in my eyes legitimate for a coding convention. – jukempff May 20 '14 at 11:09
1

The likely answer is either that they're using it to draw emphasis to the conversion or that some programmer just thought it was funny at some point -- there's absolutely no syntactical difference between !x and !!!x (or !!!!!!!!!!!!!x for that matter). After the first ! which will convert the variable to a boolean, you're just repeatedly adding the not operator to something. All that matters is whether it's negated an even or odd number of times.

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
0

That is certainly brain-pain inducing.

Normally, you would see something like the following var error_boolean = !!(error);

This is done to force "error_boolean" to either true or false.

My guess is this is a refactor bug w/ harmless consequences. Someone probably replaced something like notValue (which was double negated here), with !value. What does annotating that line of source tell you?

Or this is simply a way to garner attention at the line, its working on us so far.

sabhiram
  • 897
  • 7
  • 10
-2

!! is Cast or Convert the variable to Boolean and the additional ! inverts it. I think you could do this if it's easier to read var invertedBool = !Boolean("false");

user3653148
  • 408
  • 4
  • 3
  • No, with `!!`, the first `!` casts to a boolean and inverts it and the second reverts it to its original truthiness value. – Sam Hanley May 19 '14 at 15:15
  • 3
    A single application of the `!` operator is enough to both convert the operand to a boolean and negate it. You don't have to explicitly invoke `Boolean()`. – Frédéric Hamidi May 19 '14 at 15:16
  • @FrédéricHamidi You're right. http://jsfiddle.net/geoyws/5S5kv/1/ Perhaps they just did it for readability out of habit. But indeed ! would suffice. Ah the programmer's life. – geoyws May 19 '14 at 15:34