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?

3
  • it will cast a to bool. Commented Oct 13, 2015 at 9:15
  • It will be a boolean value in the if condition Commented Oct 13, 2015 at 9:15
  • 3
    @sobolevn: I think the point is that if already cast the expression it checks to bool in order to do the checking. So, why the !!? Commented Oct 13, 2015 at 9:16

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

IMO, anyone who writes that instead of return myObject.length > 0; deserves to be strung up by their ankles.
@Phylogenesis Haha, you're actually right, I just tried to find a !! use case ;) Do you find a better one?
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.
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>

4 Comments

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 > 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 !
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 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.