User Tools

Site Tools


java-script:double-not-operator

!! Double not operator

The double NOT operator (!!) in JavaScript is a bit of a trick that converts any value to its corresponding boolean value.
How it works:

  1. The first ! negates the value, converting it to its opposite boolean value. If the original value is truthy, this results in false, and if the original value is falsy, this results in true.
  2. The second ! negates it again, so you're back to the original truthiness of the value, but now as a boolean true or false instead of the original value.

Examples:

!!true;      // true
!!false;     // false
!!null;      // false
!!undefined; // false
!!0;         // false
!!1;         // true
!!'hello';   // true
!!'';        // false
This trick is often used when you want to ensure that a value is a boolean, and it behaves in the same way as if you had used the Boolean constructor. It's a more concise way to force a value into a boolean context, and it's used quite frequently in JavaScript code.
java-script/double-not-operator.txt · Last modified: 2023/08/10 04:13 by odefta