Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Node.js' Assert.js use !!!value in their code? What purpose does it serve? [duplicate]

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?

like image 864
geoyws Avatar asked Apr 30 '26 08:04

geoyws


2 Answers

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

like image 145
jukempff Avatar answered May 01 '26 23:05

jukempff


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.

like image 22
Sam Hanley Avatar answered May 01 '26 22:05

Sam Hanley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!