1

Within an if condidition, when does it make sense to use a double negation? I found this pattern several times:

JS

if(!!children.length) {   ...}
Zulu
  • 6,802
  • 9
  • 42
  • 51
user2952265
  • 1,430
  • 5
  • 16
  • 30
  • `!!` makes a boolean out of an expression, but in `if` is useless. – elclanrs Jan 20 '15 at 23:05
  • Refer to this [http://stackoverflow.com/questions/10467475/double-negation-in-javascript-what-is-the-purpose][1] [1]: http://stackoverflow.com/questions/10467475/double-negation-in-javascript-what-is-the-purpose – imnancysun Jan 20 '15 at 23:06
  • it never makes sense to use a boolean conversion in an IF, the IF does that for you – dandavis Jan 20 '15 at 23:07

1 Answers1

6

It's a common method to turn a normal expression to a boolean. It doesn't really make any sense inside an if statement, since it evaluates the given expression as a boolean anyways, but is useful outside.

For example:

const isEmpty = !!children.length;
Jamie Counsell
  • 6,383
  • 5
  • 36
  • 75