JavaScript enables us to use a special trick to turn any returned value into a boolean. For example, this can be very helpful for running browser feature detection scripts like the one below.
Testing for the HTML5 Canvas
if ( !!document.createElement("canvas").getContext ){
//run code for browsers that support canvas
}
In this example, document.createElement("canvas") creates dummy element that, if HTML5 is supported, inherits all the methods from the Canvas element. By testing for a Canvas method like .getContext, not .getContext() - this runs code, we can check if the real Canvas exists and is supported.
Here, the first negation converts the .getContext test result (whatever the data type might be) to a boolean. The second negation changes the boolean again to give the proper result.
Testing Negative Cases
If the value is null, undefined, false, "", or 0, then the first negation converts it to true. The second negation changes it to false.
Testing Positive Cases
If the value is object, true, "ANY VALUE” or 1, then the first negation converts it to false. The second negation changes it to true.
-
andresvidal posted this
