What does the double exclamation !! operator mean? [duplicate]
Possible Duplicate:
What is the !! operator in JavaScript? What does !! (double exclama开发者_C百科tion point) mean?
I am going through some custom JavaScript code at my workplace and I am not able to understand the following construct.
var myThemeKey = (!!$('row') && $('row').hasClassName('green-theme')) ? 'green' : 'white';
I understand everything on the above line except !!
operator. I assume that it is a NOT
operator and NOT
of NOT
is the original value but why would someone do a NOT
of NOT
?
Can someone please help me understand what is happening on the above line of code?
The !!
ensures the resulting type is a boolean (true or false).
javascript:alert("foo")
--> foo
javascript:alert(!"foo")
--> false
javascript:alert(!!"foo")
--> true
javascript:alert(!!null)
--> false
They do this to make sure $('row')
isn't null.
It's shorter to type than $('row') != null ? true : false
.
精彩评论