JavaScript NOT
JavaScript NOT Operator is used to invert the value of a boolean condition.
NOT Operator Symbol
The symbol used for NOT Operator is !
.
Syntax
The syntax to use NOT Operator with an operand a
is
</>
Copy
!a
a
can be a Boolean variable, or boolean expression, or a complex condition.
NOT Truth Table
The following truth table provides the output of NOT operator for different values of operands.
a | !a |
---|---|
true | false |
false | true |
NOT Operator negates the given value.
Examples
In the following example, we take a boolean variable with different values: true and false, and find their logical NOT output.
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var a = true;
var result = !a;
document.getElementById('output').innerHTML += '\n!' + a + ' is ' + result;
a = false;
result = !a;
document.getElementById('output').innerHTML += '\n!' + a + ' is ' + result;
</script>
</body>
</html>
In the following example, we will use NOT operator for a boolean condition in If Statement.
index.html
<!DOCTYPE html>
<html lang="en">
<body>
<pre id="output"></pre>
<script>
var a = 4;
if (!(a % 2 == 0)) {
document.getElementById('output').innerHTML += a + ' is not even.';
} else {
document.getElementById('output').innerHTML += a + ' is even.';
}
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned about Logical NOT Operator, its syntax, and usage with examples.