Double exclamation points?

Javascript

Javascript Problem Overview


> Possible Duplicate:
> What is the !! (not not) operator in JavaScript?
> What does the !! operator (double exclamation point) mean in JavaScript?

So I was debuging some code and ran across this:

var foo.bar = 0; // this is actually passed from another function, adding it for context

function(foo) {
    var someVar = !!foo.bar;
    
    if (foo.bar) {
      // ..stuff happens
    } else {
      // .. something else happens
    }
}

Okay my questions is what is the point of !!? All that is doing is making the 0 === false.

  1. Is there any benefit to using that compared to boolean(foo.bar)?

  2. foo.bar can be evaluated in an if as is because 0 === false already, so why go through the conversion? (someVar is not reused anywhere else)

Javascript Solutions


Solution 1 - Javascript

This converts a value to a boolean and ensures a boolean type.

"foo"      // Evaluates to "foo".
!"foo"     // Evaluates to false.
!!"foo"    // Evaluates to true.

If foo.bar is passed through, then it may not be 0 but some other falsy value. See the following truth table:

Truth Table for javascript

''        ==   '0'           // false
0         ==   ''            // true
0         ==   '0'           // true
false     ==   'false'       // false
false     ==   '0'           // true
false     ==   undefined     // false
false     ==   null          // false
null      ==   undefined     // true
" \t\r\n" ==   0             // true

> Source: Doug Crockford

Javascript also gets really weird when it comes to NaN values. And this is the only case I can think of off the top of my head where !! would behave differently to ===.

NaN   ===  NaN     //false
!!NaN === !!NaN    //true

// !!NaN is false

Solution 2 - Javascript

I think the answer is that there isn't really much point. We can speculate about how it came about:

  • maybe an earlier version of the function used someVar in multiple places, or in ways that genuinely benefited from having true or false, so this made more sense.
  • maybe the person who wrote the function is so used to using !! to convert to true/false that (s)he didn't even notice that it wasn't necessary here.
  • maybe the person who wrote the function feels that every computation (in this case, Boolean conversion) should be given a meaningful name by assigning some variable to its result.
  • maybe, since Boolean conversion in JavaScript is surprisingly error-prone (in that e.g. new Boolean(false) is a true-valued value), the person who wrote the function feels that it should always be done explicitly rather than implicitly — even though the effect is the same — just to call attention to it as a potential point of error.
    • this, of course, presupposes that the person who wrote the function thinks of !! as an "explicit" Boolean conversion. Technically it's not — it uses the same implicit Boolean conversion that if does — but if you're used to this idiom, then it amounts to an explicit conversion.

but in my subjective opinion, none of those reasons is a very good one!

Solution 3 - Javascript

As stated above, it forces an object with a boolean type. You can see for yourself:

    (function typecheck() {
      var a = "a";
      var b = !a;
      var c = !!a;
    
      console.log("var a =", a, typeof(a))
      console.log("var b =", b, typeof(b))
      console.log("var c =", c, typeof(c))
    })();

If you are simply doing comparisons, the conversion merely saves you a type coercion later on.

FYI, the following values are coerced to FALSE in JavaScript:

  • false
  • 0
  • ""
  • null
  • undefined

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjpalladino84View Question on Stackoverflow
Solution 1 - JavascriptGazlerView Answer on Stackoverflow
Solution 2 - JavascriptruakhView Answer on Stackoverflow
Solution 3 - JavascriptMatt BrockView Answer on Stackoverflow