Saturday, September 27, 2014

JavaScript !! - bang, bang

The !! construct is a simple way of turning any JavaScript expression into its Boolean equivalent.

legendary Cast-to-bool 'operator',  "bang, bang you're boolean"

Coerces Object to boolean. If it was falsey (e.g. 0, nullundefined, etc.), it will be false, otherwise, true.

!oObject  //Inverted boolean
!!oObject //!! converts the value to the right of it to its equivalent boolean value

true === !!10
false === !!0
So !! is not an operator, it's just the ! operator twice. It is the double-use of ! -- which is the logical "not" operator.
The "falsey" values are:
  • false
  • NaN
  • undefined
  • null
  • "" (empty string)
  • 0

No comments:

Post a Comment