12

!!x coerces the type of variable x to a boolean, whilst maintaining its truthiness or lack thereof - see this question - I have a question about the use of this in conditional expressions.

A few times in JS code I've seen !! used to coerce a variable to boolean type in an if condition like so

if(!!x) {
    x.doStuff();
}

Where the idea is to test if x is defined before calling methods on it.

But, in my own code I've always just used

if(x) {
    x.doStuff();
}

On the premise that if x is defined, the condition will pass, and if x is undefined, it will not pass.

So my question is what is the point of coercing x to a boolean using !! in this scenario? What does this code do that this code doesn't?

davnicwil
  • 28,487
  • 16
  • 107
  • 123
  • 1
    No, I have linked to that question and understand the answer, I am asking what is the point in coercing the type to boolean in this scenario - please read the question before flagging as a duplicate! – davnicwil Sep 06 '13 at 01:00
  • 2
    The answer is that there is no point in doing that. It's entirely the same as if you left off the `!!`. – user2736012 Sep 06 '13 at 01:02
  • 1
    Indeed. People do all kinds of superstitious things while learning programming. – Greg Hewgill Sep 06 '13 at 01:03
  • Yikes, where did you see that? – Ry- Sep 06 '13 at 01:07
  • The only time you should ever come across a !! in JS is when you are checking equality (or will in the future). if(!!x) is pointless. – Adam Jenkins Sep 06 '13 at 01:34
  • So @user2736012 + Adam - it may be totally redundant in this case - that's what I immediately think, too - again I don't use this, but have seen it used in other people's code. Seems like a pretty purposeful thing to do, which leads me to suspect there could be edge cases I'd not thought of where it might have a purpose. On the other hand, maybe it's an oversight, bad refactoring, or just a plain old mistake! – davnicwil Sep 06 '13 at 02:16
  • @davnicwil: When you provide an expression for the `if()` condition, the first thing that happens after the expression is evaluated is that a `ToBoolean` conversion is done. That's the same conversion that is done with the `!` operator. Either it's an oversight, or maybe it's some machine generated code that for some reason outputs explicit type conversions. – user2736012 Sep 06 '13 at 08:00
  • @user2736012 yeah, so it seems pretty watertight that there's no scenario where this could be anything other than redundant? Consider promoting comment to answer :-) – davnicwil Sep 06 '13 at 08:05
  • See also [Why use `if (!!err)`?](https://stackoverflow.com/q/27257803/1048572), [Why use `!!` to coerce a variable to boolean for use in a conditional expression?](https://stackoverflow.com/q/18648179/1048572) and [When to use the double not (`!!`) operator in JavaScript](https://stackoverflow.com/q/2174297/1048572) – Bergi Aug 04 '17 at 06:53

3 Answers3

11

In that specific context, I would say that there is no difference between explicitely converting to boolean using !! or let the if expression being converted to a boolean naturally. What I mean by this is that if (x) will be interpreted as if (Boolean(x)), which is the same as if (!!x).

However, if you are returning a value from a function, for instance if you want to implement a arrayHasItems function, you could implement it this way:

function arrayHasItems(arr) {
    return arr.length;
}

Using the function in a if statement as is would work because the numerical value returned from the function would be converted to a boolean value. However, the client code expects the function to return a boolean value, so he might be checking the condition by doing:

if (arrayHasItems(arr) === true) {}

In this case it would fail, because the returned result from arrayHasItems was a number. Therefore, it would have been better to implement the function by returning a boolean like expected.

function arrayHasItems(arr) {
    return !!arr.length;
}

EDIT:

This brings up a new question: why !!arr.length and not just arr.length > 0

There isin't any difference between both in the result produced and you are not even saving bytes since both statements take the same amount of characters. However I created a test case and the double negation seems to perform better, but it might not be consistent across all browsers.

plalx
  • 42,889
  • 6
  • 74
  • 90
0

As stated it converts a value to a boolean value and is therefore not much different from if(x). However, both are tricky as it will also convert 0 to false and other such things.

ced-b
  • 3,957
  • 1
  • 27
  • 39
  • 3
    I don't see how your examples have anything to do with the question. – Smern Sep 06 '13 at 01:02
  • `if (myValue !== null && myValue !== undefined)` is not the same as `if (myValue) {}` – Dmitry S. Sep 06 '13 at 01:05
  • Ok, removed the examples. The point I was trying to make is don't do null checks just by checking `if(x)` or `if(!!x)` because it is up to ambiguity. – ced-b Sep 06 '13 at 01:08
0

It has to do with JavaScript type coersion.

Often you want to check if an object is not null, not undefined, not 0, etc. The common way to check for that is

if (obj) {
  ...
}

However, if you want the condition to be equal to true or false the ! can be used on non-boolean object instances. Here is an example that uses an equality operator === that does not do type coersion.

var obj = {};

console.log(obj === true); // returns false
console.log(!obj === false); // returns true
console.log(!!obj === true); // returns true
Dmitry S.
  • 8,373
  • 2
  • 39
  • 49