What is !! in javascript?
Possible Duplicate:
What does the !! operator (double exclamation point) mean in JavaScript?
$("#imjavascript").attr('checked', !!$('#mainCheck').attr('checked'));
what do !! mean ?
It's a double-negation or double-bang as some call it (possibly/probably other names as well), it's getting the property and converting it to a boolean. The first !
takes the inverse of the value - resulting in a boolean, then the second takes the inverse of that, so you get a boolean back that's a true
/ false
representation of the original, not inverse of the original.
It's an idiomatic method of type changing a boolean convertible non-boolean type to an actual boolean type. For example, 0 is of Number type, but is also considered to be truth equivalent to the boolean value "False". Negation is an involute operation (ie it inverts itself), so by using double negation, we get an the same truth value back, but of native boolean type.
Concretely, consider !(!0)
, which evaluates to !(True)
(as !0
is True) which evaluates to False
- the same truth value as 0, but now an actual boolean.
上一篇: 为什么“true”== true在JavaScript中显示false?
下一篇: 什么是 !! 在JavaScript?