Monday, January 6, 2014

!! Negation in Javascript

Double negation casts a string or a number to a boolean:
var bool = !!num;
The following values are equivalent to false in conditional statements:
  • false
  • null
  • undefined
  • The empty string "" (\ '')
  • The number 0
  • The number NaN
All other values are equivalent to true.
var x = "somevalue" var isNotEmpty = !!x.length;
Let’s break it to pieces:
x.length   // 9
!x.length  // false
!!x.length // true
 

No comments :