Logical NOT Operator
Logical NOT operator takes an operand, and returns true if given operand if false, or false if given operand if true.
Syntax
!
symbol is used for logical NOT operator.
!a
Truth Table of NOT Gate
The following truth table provides the output of NOT operator for possible operand values.
a | !a |
---|---|
true | false |
false | true |
Examples
1. Check if value in num
is not positive.
let num = -11;
if (!(num > 0)) {
console.log('num is not positive');
} else {
console.log('num is positive');
}
in the above example, operand a
is (num > 0)
which is to check if num
is positive, but the condition that we need to check is if num
is not positive. so, we use not operator to negate the condition (num > 0)
, thus our required condition of checking if num
is not positive happens.