0

If I have an if statement that doesn't have a conditional operator, does it matter if I use the double exclamation mark operator - !!?

For example...

if ([]) {

}

vs...

if (!!([])) {

}

From what I understand - the operator is basically asking - "is this value truthy?". So it is redundant in this case. Is it redundant in all cases similar to this?

EDIT:

if (x) { console.log("hi"); }
if (!!x) { console.log("hi"); }

Will both of these print for any x? That is my question.

Ogen
  • 6,396
  • 5
  • 46
  • 119

1 Answers1

2

Yes. Both of these will print for any Boolean(x) === true

For more detail, you can find truthy, falsy and Boolean in JavaScript

For esier to understand, you can use Boolean([]) // true

taile
  • 2,598
  • 16
  • 28