Double negation (!!x)

suggest change

The double-negation !! is not a distinct JavaScript operator nor a special syntax but rather just a sequence of two negations. It is used to convert the value of any type to its appropriate true or false Boolean value depending on whether it is truthy or falsy.

!!1            // true
!!0            // false
!!undefined    // false
!!{}           // true
!![]           // true

The first negation converts a truthy value to false and a falsy value to true. The second negation turns that Boolean back into the truthiness of the original value. The result matches Boolean(x).

!!x is easy to misread. Boolean(x) is the same conversion. These tests are not substitutes for it: x !== 0 is true for NaN, and x != null is true for 0 and "".

Usage of !!x is considered poor practice due to the following reasons:

  1. Stylistically it may look like a distinct special syntax whereas in fact it is not doing anything other than two consecutive negations with implicit type conversion.
  2. It is better to provide information about types of values stored in variables and properties through the code. For example, x !== 0 says that x is probably a number, whereas !!x does not convey any such advantage to readers of the code.
  3. Usage of Boolean(x) allows for similar functionality, and is a more explicit conversion of type.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents