Knowing that !!foo
gives you the Boolean value of foo
, I have seen some programmers say that it's better to use !!!foo
instead of !foo
because you are changing it to its Boolean value first and then negating the value.
So my question is,
Is !!!foo
always equals !foo
? (!!!foo === !foo
)
Yup. Just for clarity:
!!x === x
is not generally true, but it is true if x
is already a boolean: "not (not true)" is true, and "not (not false)" is false.
!foo
is always a boolean; if foo
is truthy, it's false
, otherwise it's true
.
So if you substitute !foo
in for x
you get that !!(!foo) === (!foo)
is always true. Removing the parentheses doesn't change the meaning, so !!!foo === !foo
is always true.
Which means there's no good reason to write !!!foo
in actual code. Just use !foo
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With