What is the "!!" operator in Javascript?
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? eg
if (!!window.EventSource) { var source = new EventSource('stream.php'); } else { // Result to xhr polling :( }
Did the author just use "!" twice ie 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
!! is used to convert a non-zero/non-null value to boolean true and a zero/null value to false.
Eg if a = 4, then !a = false and !!a = !(!a) = true.
链接地址: http://www.djcxy.com/p/12656.html上一篇: 的意思 !! JavaScript的