9

Can I get some clarification on why I would want to use this?

myVar = !!someOtherVar;
jerome
  • 4,809
  • 13
  • 53
  • 70

4 Answers4

12

In non-strictly typed languages, the ! operator converts a value to a boolean. Doing it twice would be equivalent to saying

myVar = (boolean)someOtherVar

Note that this is not recommended for code clarity.

phsource
  • 2,326
  • 21
  • 32
  • 11
    ... or is it not not unrecommended? – STW Sep 22 '10 at 18:41
  • @STW: http://www.youtube.com/watch?v=0QOya9-lwQk (see 1:44 and 8:38) – BoltClock Sep 22 '10 at 18:50
  • 3
    You can type cast in JS using `Boolean(someOtherVar)` – MooGoo Sep 22 '10 at 18:50
  • @BoltClock -- by the first minute I was having flashbacks to the South Park where Butters becomes a pimp; you know what I'm sayin? – STW Sep 22 '10 at 18:58
  • @MooGoo -- thanks for my "how didn't I know that exists?" moment of the day – STW Sep 22 '10 at 19:01
  • @MooGoo that is **not** a "type cast" - that's an object construction, and not the same thing at all. – Pointy Sep 22 '10 at 19:08
  • @Pointy: in which situations does the difference between `Boolean()` and `!!` matter? – chelmertz Sep 22 '10 at 19:16
  • @chlmertz, @MooGoo - you're right; when the `Boolean()` constructor is *called* (as opposed to being invoked with `new`), it seems to return either `true` or `false` - in other words, the constants, and not an object. Thus `true === Boolean(2)` is `true`, while `true === new Boolean(2)` is `false`. The MDC page pretty much says exactly this (and leaves one wondering just what use the objects might be). – Pointy Sep 22 '10 at 19:28
  • @chimetz, @MooGoo I personally still wouldn't use the term "type cast", but I guess that's just a matter of preference. – Pointy Sep 22 '10 at 19:29
  • It creates a bona-fide object that wraps the primitive value. As far as uses go, there's not many. You can add properties to the object (whereas with a primitive any added properties would be ignored), but it is useless in comparisons, because objects are only == to themselves in JS. I would say "type cast" is appropriate because it describes the behavior exactly, even if the implementation here is different than in most other languages (calling a function that returns a value vs special syntax) – MooGoo Sep 22 '10 at 19:44
7

(Rewritten to clarify, simplify)

That statement performs a couple different actions:

myVar = // This portion is a regular assignment, it will store the value of the suffix
        !!someOtherVar; // This portion is evaluated to a boolean result

The !!someOtherVar, I assume, is what you're really asking about. The answer is simple: it performs two logical NOT operations against the truthiness (a Javascript'ism) of someOtherVar.

In other words, if you understand the ! operator, this just combines two of them (!! isn't a different operator). By doing this it essentially returns the boolean evaluation of someOtherVar--in other words, it's a cast from whatever type someOtherVar is to boolean.

So... to walk through this, and pay attention to the result of myVar:

myVar = someOtherVar; // myVar will be whatever type someOtherVar is
myVar = !someOtherVar; // myVar will *always be boolean, but the inverse of someOtherVar's truthiness
myVar = !!someOtherVar; // myVar will *always be boolean, and be the equivalent of someOtherVar's truthiness
STW
  • 44,917
  • 17
  • 105
  • 161
7

It's a double negation, but it also works for type casting. !somevar will return a boolean (true, if somevar is "falsey" and false if it is "truthy", as per Crockford's lectures). So, !!somevar will be not(bool) and hence it will be boolean.

Niqql
  • 410
  • 1
  • 5
  • 26
naivists
  • 32,681
  • 5
  • 61
  • 85
4

If you need to pass a boolean value to a function, or are anal about evaluating only booleans in conditional statements, that casts someOtherVar to a boolean for you by double-negating it.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356