1

I have seen JS code written using

if(!!a){
 //something here
 console.log('something');
}

I don't understand what the preference of doing this is compared to:

if(a){
//something else here
 console.log('something else here');
}

Do you gain anything by typing !! in the expression with JS?

Cisum Inas
  • 11,552
  • 11
  • 40
  • 55

3 Answers3

2

It's about truthy and falsy:

Everything in JavaScript has an inherent Boolean value, generally known as either truthy or falsy.

The following values are always falsy:

  • false
  • 0
  • ""
  • null
  • undefined
  • NaN

All other values are truthy, including "0" (zero in quotes), "false" (false in quotes), empty functions, empty arrays, and empty objects.

In your case, I don't think it is useful, since an if already uses the truthy/falsy value of the condition:

if (0) {
  // this will never be executed
}

The !! can be used like this:

return !!myObject.length; // returns true if myObject.length > 0, false otherwise
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • 3
    IMO, anyone who writes that instead of `return myObject.length > 0;` deserves to be strung up by their ankles. – Phylogenesis Oct 13 '15 at 09:26
  • @Phylogenesis Haha, you're actually right, I just tried to find a `!!` use case ;) Do you find a better one? – sp00m Oct 13 '15 at 09:27
  • I can't think of a good one (in fact I'm not totally convinced there even is one). I see it used for brevity at the expense of readability a lot, however. – Phylogenesis Oct 13 '15 at 09:31
2

The if statement checks for the truthiness of the expression passed to it. The !! coerces truthiness into boolean. Therefore doing:

if (!!a) {}

is exactly the same as:

if (a) {}

There is nothing to be gained from using !! in this case

This smells strongly of cargo-cult programming or influence form another language.

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • Unless someone can come up with an example where it makes a difference. I strongly believe there are none. – slebetman Oct 13 '15 at 09:21
0

Some samples to understand :

var el = document.getElementById('el');
var log = function(val){ 
//  el.innerHTLM+='<div><pre>' + JSON.stringify(val , null , ' ') +'<\pre></div>';
  el.innerHTML+='<div><pre>' + val  +'<\pre></div>';

};

var a = 'Helo';

log('a = ' +  a);
log('! a = ' +  ! a);
log(' !! a = ' + !!a );
log('---');
log('true =' + true);
log('!true =' + !true);
log('!!true =' + !!true);
log('---');
log('false =' + false);
log('!false =' + !false);
log('!!false =' + !!false);
log('---');
log('null =' + null);
log('!null =' + !null);
log('!!null =' + !!null);
log('---')
log('undefined =' + undefined);
log('!undefined =' + !undefined);
log('!!undefined =' + !!undefined);
log('0 =' + 0);
log('!0 =' + !0);
log('!!0 =' + !!0);
log('---');
log('1 =' + 1);
log('!1 =' + !1);
log('!!1 =' + !!1);
log('---');
log('[] =' + []);
log('![] =' + ![]);
log('!![] =' + !![]);
log('---');
log('{} =' + {});
log('!{} =' + !{});
log('!!{} =' + !!{});
<div id='el'>Use case:</div>
Anonymous0day
  • 3,012
  • 1
  • 14
  • 16
  • 1
    Although you're showing what `!!` does, you haven't actually answered the question "Do you gain anything by typing !! in the expression with JS?" - i.e., is there any point in using it in an if condition? – daiscog Oct 13 '15 at 09:43
  • @daiscog > Some samples to understand < what can i add more ???? i'am just showing what !! do, to help in understanding the behaviour : `is there any point in using it` yes millions, you can use it now because you saw the beahviour ! – Anonymous0day Oct 13 '15 at 13:56
  • It is a useful illustration of what `!!` does, but the question was that since everything in JS has an inherent truthy value anyway, is there any point in using it _in an `if` condition_ (i.e., are there any situations where using it would give a different result in the `if` condition than not using it). The answer to that is quite simply "no". It's completely redundant in an if condition. To be honest, your answer does sort of show that, but without actually saying it explicitly. – daiscog Oct 19 '15 at 10:02
  • @daiscog It is ***not*** an answer !!! it is > Some samples to understand < i should write > this is not an answer , just some samples to see some results < You are right it would have been much clearer, even if it does not change much, really. – Anonymous0day Oct 19 '15 at 13:21