Possible Duplicate:
What is the !! operator in JavaScript?
Sorry if this one is obvious but I can't google it.
What is the "!!" operator in Javascript? e.g.
if (!!window.EventSource) {
var source = new EventSource('stream.php');
} else {
// Result to xhr polling :(
}
Did the author just use "!" twice i.e. a double negation? I'm confused because this is in the official doc.
It will convert anything to true
or false
:
!!0 // => false
!!1 // => true
!!'a' // => true
!!'' // => false
!!null // => false
Technically, !!
is not an operator, it is just two !
operators next to each other. But a double-negation is pointless unless you are using !!
like an operator to convert to Boolean type.
In most languages, !!
is double negation, as !
is negation. Consider this:
# We know that...
!false == true
# And therefore...
!!false == false
!!true == true
It's often used to check whether a value exists and is not false, as such:
!!'some string' == true
!!123 == true
!!myVar == true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With