Friday 15 January 2010

javascript - Is !! a best practice to check a truthy value -



javascript - Is !! a best practice to check a truthy value -

in angular.js, there code snippets utilize !! check whether value truthy in if condition. best practice? understand in homecoming value or other assignment !! used create sure type boolean. true status checks?

class="snippet-code-js lang-js prettyprint-override">if (!!value) { element[name] = true; element.setattribute(name, lowercasedname); } else { element[name] = false; element.removeattribute(lowercasedname); }

!!value commonly used way coerce value either true or false, depending on whether truthy or falsey, respectively.

in command flow statement such if (value) { ... } or while (value) { ... }, prefixing value !! has no effect, because command flow statement already, definition, coercing value either true or false. same goes status in ternary operator look value ? : b.

using !!value coerce value true or false idiomatic, should of course of study done when isn't made redundant accompanying language construct.

javascript angularjs

No comments:

Post a Comment