Is !! a best practice to check a truthy value in an if statement
Asked Answered
R

2

31

In angular.js, there are some code snippets use !! to check whether a value is truthy in if condition.

Is it a best practice? I fully understand in return value or other assignment !! is used to make sure the type is Boolean. But is it also true for condition checks?

if (!!value) {
  element[name] = true;
  element.setAttribute(name, lowercasedName);
} else {
  element[name] = false;
  element.removeAttribute(lowercasedName);
}
Resht answered 13/11, 2014 at 10:12 Comment(3)
see #785429Ostensorium
@Ostensorium This doesn't answerNotum
this is certainly not best practice, if anything it's a bad practice because it confuses readers and does not have any practical use.Monomerous
R
46

No, !! is totally useless in a if condition and only confuses the reader.

Values which are translated to true in !!value also pass the if test because they're the values that are evaluated to true in a Boolean context, they're called "truthy".

So just use

if (value) {
Ruthy answered 13/11, 2014 at 10:12 Comment(4)
Yes, it's useless in javascript.Pointtopoint
Agreed w/r/t control flow, but there is use in doing this in the context of a strongly typed system, such as Flow or TypeScipt. I.e. in cases where a function's signature strictly necessitates a boolean type.Mechanistic
@Mechanistic There are often cases where you need a boolean, even in JavaScript. You may also want to provide a boolean to an API managed by somebody's else because it's cleaner and it lets you not have to check how the value is interpreted. When I say it's useless I'm only specifically referring to the use in a if condition.Notum
Thanks for the clarification :)Mechanistic
G
60

!!value is commonly used as a way to coerce value to be either true or false, depending on whether it is truthy or falsey, respectively.

In a control flow statement such as if (value) { ... } or while (value) { ... }, prefixing value with !! has no effect, because the control flow statement is already, by definition, coercing the value to be either true or false. The same goes for the condition in a ternary operator expression value ? a : b.

Using !!value to coerce value to true or false is idiomatic, but should of course only be done when it isn't made redundant by the accompanying language construct.

Gearing answered 13/11, 2014 at 19:42 Comment(3)
This is a better answer then the other one which states that it is totally useless. As you point out, there is a use case for those rare times when you do need to coerce the expression to true or false, for example when a function expects a boolean argument.Blasphemy
@SunilD. Please read the question against : " in if condition".Notum
Is this a valid/proper use case for !! const validData = [value1, value2, .....].filter(val => !!val); where validData is an array having only the truthy valuesMuscarine
R
46

No, !! is totally useless in a if condition and only confuses the reader.

Values which are translated to true in !!value also pass the if test because they're the values that are evaluated to true in a Boolean context, they're called "truthy".

So just use

if (value) {
Ruthy answered 13/11, 2014 at 10:12 Comment(4)
Yes, it's useless in javascript.Pointtopoint
Agreed w/r/t control flow, but there is use in doing this in the context of a strongly typed system, such as Flow or TypeScipt. I.e. in cases where a function's signature strictly necessitates a boolean type.Mechanistic
@Mechanistic There are often cases where you need a boolean, even in JavaScript. You may also want to provide a boolean to an API managed by somebody's else because it's cleaner and it lets you not have to check how the value is interpreted. When I say it's useless I'm only specifically referring to the use in a if condition.Notum
Thanks for the clarification :)Mechanistic

© 2022 - 2024 — McMap. All rights reserved.