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:
Operand | Result |
---|---|
an object | false |
empty string("") | true |
nonempty string | false |
0 | true |
any number other than 0 (including Infinity) | false |
null | true |
NaN | true |
undefined | true |
<!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>
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>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Operators:
- JavaScript Operators
- Increment/Decrement Operators
- Increment/Decrement Operators for String, Boolean, Floating-point and Object
- Unary Plus and Minus
- Bitwise Not operator
- Bitwise AND
- Bitwise OR
- Bitwise XOR
- Left Shift
- Signed Right Shift
- Unsigned Right Shift
- Logical NOT
- Logical AND
- Logical OR
- Multiply
- Divide
- Modulus
- Add
- Subtract
- Relational Operators
- Equal and Not Equal
- Identically Equal and Not Identically Equal
- Conditional Operator
- Assignment Operators
- Comma Operator