4

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)

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Vencovsky
  • 28,550
  • 17
  • 109
  • 176
  • 7
    That's silly. A single application of `!` will convert the value to boolean and result in the logical negation of that. The result is **always** either `true` or `false`. There's no reason to apply `!` twice more. – Pointy Dec 17 '19 at 14:08
  • 2
    Yes. (and some more letters, because the comment has a minimum length). – ASDFGerte Dec 17 '19 at 14:08
  • I know it's silly, I just needed to ask this just to be sure... some people don't see that it's the same thing – Vencovsky Dec 17 '19 at 14:10
  • 2
    It's a particularly indecisive boolean, one that's afraid to make the hard decisions in life. –  Dec 17 '19 at 14:11
  • while it may be a somewhat silly question, logical not is actually one of the very few things in JS, that allow no language abuse to get even close to changing the result, after the first step. There are ways to get e.g. `a !== a` to work, you can do weird things if results are strings (or even better, objects), but boolean not, no chance. – ASDFGerte Dec 17 '19 at 14:12
  • Very simply, if you know what one exclamation point does, adding more just switches between true and false. `!!foo` doesn't give you "the Boolean value" of `foo`, it gives you the negation of the negation of the coercion of `foo` to a Boolean. – Heretic Monkey Dec 17 '19 at 14:15
  • Does this answer your question? [The use of the triple exclamation mark](https://stackoverflow.com/questions/21154510/the-use-of-the-triple-exclamation-mark) – iPhoenix Dec 17 '19 at 14:30

4 Answers4

5

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.

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
3

I've never seen !!!foo used in actual code, but yes, they are always strictly equal.

The extra two exclamation marks just negate the boolean value twice, which always gives the same result.

iPhoenix
  • 719
  • 7
  • 20
1

If foo is set as boolean then !foo will be equal to !!!foo , See the example and screenshot given below

var foo = true;

console.log('foo => '+foo);
console.log('!foo => '+!foo);
console.log('!!foo => '+!!foo);
console.log('!!!foo => '+!!!foo);
console.log('!!!!foo => '+!!!!foo);
if(!!!foo === !foo){
  console.log("Equals");
}else{
  console.log("Not Equals");
}

enter image description here

Ajith
  • 2,476
  • 2
  • 17
  • 38
0

While you are correct, !!!foo would equal to !foo, I've never seen JS script using !!!foo before, and I can't think of a good reason to do that. You're gonna create code smell if you write this in your own code.

In other words, don't do that.

fdrobidoux
  • 274
  • 4
  • 11