I just ran into this
var strict = !! argStrict;
I can't help but wonder, what is the meaning of !!
? Is it a double negative? Seems pretty redundant, not likely that the guys from php.js
would use that in such case?
I just ran into this
var strict = !! argStrict;
I can't help but wonder, what is the meaning of !!
? Is it a double negative? Seems pretty redundant, not likely that the guys from php.js
would use that in such case?
It forces the type to become a true boolean value rather than a "truthy" value. Examples:
var a = (1 === true) // comes out as false because 1 and true are different types and not exactly the same
var b = ((!!1) === true) // comes out as true because the 1 is first converted to a boolean value by means of negation (becomes false) and then negated a second time (becomes true)
It means basically "convert to boolean".
It's negating it twice, so it argStrict
is "falsy", then !argStrict
is true, and !!argStrict
is false
.
It returns a boolean
value. false
for undefined
, null
, 0
, ''
and true
any truthy
value.
This is an example of slacker parsing.
If you have a variable, for example a string, and you want to convert it into a boolean, you could do this:
if (myString) {
result = true;
}
This says, if myString is NOT undefined, null, empty, the number 0, the boolean false then set my string to the boolean value to true...
But it is faster, and way cooler to double bang it:
result = !! myString;
Other examples include....
//Convert to number
result = +myString;
//Bang Bang Wiggle - coerces an indexOf check into a boolean
result !!~myString.indexOf('e');