Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  12] [ 3]  / answers: 1 / hits: 32056  / 10 Years ago, thu, november 13, 2014, 12:00:00

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);
}




More From » angularjs

 Answers
3

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) {

[#68820] Monday, November 10, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackie

Total Points: 442
Total Questions: 107
Total Answers: 94

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
jackie questions
Sat, Sep 18, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
;