Logical NOT Operator
logical NOT operator takes an operand, and returns true if given operand if false, or false if given operand if true
!
symbol is used for logical NOT operator
!a
the following truth table provides the output of NOT operator for possible operand values
a | !a |
---|---|
true | false |
false | true |
Examples
1. check if 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