Logical NOT

The logical NOT operator is !. The logical NOT operator may be applied to any value in JavaScript. This operator always returns a Boolean value regardless of the data type.

The logical NOT operator first converts the operand to a Boolean value and then negates it.

The following table lists the data types and its corresponding result for logical not operator:

OperandResult
an objectfalse
empty string("")true
nonempty stringfalse
0true
any number other than 0 (including Infinity)false
nulltrue
NaNtrue
undefinedtrue
 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
    
        document.writeln(!false); //true 
        document.writeln(!"asdf"); //false 
        document.writeln(!0); //true 
        document.writeln(!NaN); //true 
        document.writeln(!""); //true 
        document.writeln(!12345); //false 

       
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo

Boolean() vs !!

By using two NOT operators(!!), you can simulate the behavior of the Boolean() casting function.

 
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <script type="text/javascript">
        document.writeln(!!"asdf"); //true 
        document.writeln(!!0); //false 
        document.writeln(!!NaN); //false 
        document.writeln(!!""); //false 
        document.writeln(!!12345); //true 
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Operators:
  1. JavaScript Operators
  2. Increment/Decrement Operators
  3. Increment/Decrement Operators for String, Boolean, Floating-point and Object
  4. Unary Plus and Minus
  5. Bitwise Not operator
  6. Bitwise AND
  7. Bitwise OR
  8. Bitwise XOR
  9. Left Shift
  10. Signed Right Shift
  11. Unsigned Right Shift
  12. Logical NOT
  13. Logical AND
  14. Logical OR
  15. Multiply
  16. Divide
  17. Modulus
  18. Add
  19. Subtract
  20. Relational Operators
  21. Equal and Not Equal
  22. Identically Equal and Not Identically Equal
  23. Conditional Operator
  24. Assignment Operators
  25. Comma Operator