-4

If a value must be defined to true or false, based off the check off another value, are there any gotchas in doing :

!!someValue

Just a quick run against chrome's engine gives:

boolCheck=function(someValue){
    return !! someValue;
}

console.log(boolCheck());           //false
console.log(boolCheck(undefined));  //false
console.log(boolCheck(null));       //false
console.log(boolCheck(1));          //true
console.log(boolCheck({va:1}));     //true
console.log(boolCheck("someTS"));   //true
console.log(boolCheck(boolCheck));  //true    

which SEEMS to work....

Alex
  • 5,674
  • 7
  • 42
  • 65
  • What are you asking here? What is true and what is false in a boolean context is well-defined. Not sure what "gotchas" you are seeking. – Jeremy Roman Oct 19 '12 at 14:25
  • What on earth do you mean gotchas in doing? Why are we looking for gotchas in something that makes no sense to do in the first place? – djechlin Oct 19 '12 at 14:25
  • see also: http://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript – georg Oct 19 '12 at 14:26
  • 3
    You should use !!!!!!!!!!someValue to make it cooler! – skovalyov Oct 19 '12 at 14:26
  • 1
    @skovalyov Psh, I use double the amount of `!` than that! – Ian Oct 19 '12 at 14:26
  • If depends on what is "normal" to you or not. In Python, an empty list or dictionary (Javascript array or object, respectively) or string is technically "False". That doesn't seem to be the same for Javascript - try passing `[]` to `boolCheck`. – Ian Oct 19 '12 at 14:28
  • 1
    most sane people prefer `Boolean(x)` to `!!x`, as it is more straight-forward and `!!x` just calls the `Boolean` constructor internally anyways. – jbabey Oct 19 '12 at 14:39
  • @jbabey: It does an internal *ToBoolean* conversion, but I doubt that any implementation would actually call the constructor function. – I Hate Lazy Oct 19 '12 at 14:51

3 Answers3

6

The ! operator always returns either true or false, so assuming evaluation of x completes normally, !!x is a boolean value that is the equivalent of Boolean(x) where Boolean is the EcmaScript builtin function.

http://es5.github.com/#x11.4.9 says

11.4.9 Logical NOT Operator ( ! )

The production UnaryExpression : ! UnaryExpression is evaluated as follows:

  1. Let expr be the result of evaluating UnaryExpression.
  2. Let oldValue be ToBoolean(GetValue(expr)).
  3. If oldValue is true, return false.
  4. Return true.

http://es5.github.com/#x9.2 explains how ToBoolean works

9.2 ToBoolean

The abstract operation ToBoolean converts its argument to a value of type Boolean according to Table 11:

Table 11 — ToBoolean Conversions

Argument Type    Result 
Undefined        false
Null             false
Boolean          The result equals the input argument (no conversion).
Number           The result is false if the argument is +0, −0, or NaN; otherwise the result is true.
String           The result is false if the argument is the empty String (its length is zero); otherwise the result is true.
Object           true

The above table explains most of your examples, but it may not be obvious why

 console.log(boolCheck());

is false. When you call a function with fewer actual arguments than formal parameters, the extra parameters have the value undefined and, as the table above shows !!undefined is false.


are there any gotchas in doing : !!someValue?

The intent is less clear than Boolean(someValue), but it will work consistently across platforms and most experienced Javascript devs will recognize it.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
0

Javascript is not strongly typed, so if you not some value it will produce different results depending on the type passed in. So yes, there are plenty of gotchas!

Liam
  • 27,717
  • 28
  • 128
  • 190
0

That's basically means that if it's 0, null, undefined, or false then evaluate to false, anything else evaluate to true.

Which, by the way, if being used in a situation that is automatically being cast to a boolean is the exact same as just evaluating the variable itself. "if(someValue){}" will be the same as "if(!!someValue){}" because the if statement will automatically evaluate the contents to a boolean, and the !! is just doing the same thing (redundantly in that situation). But the plain fact is that 99.9999% of the time when you're using any sort of boolean it will eventually be evaluated in that sort of way, which is why !! really has very little place in code except to be redundant.

But in the end it's your definition of "gotchas" that is too vague to be understood here. I have no freakin idea if "null" evaluating to "false" will break your intended usage or not!

Jimbo Jonny
  • 3,549
  • 1
  • 19
  • 23