|Written by Ian Elliot|
|Thursday, 29 November 2018|
Page 3 of 3
The NOT !
Finally we come to the NOT ! operator.
This works in the same way as the other logical operators but it only has a single operand and also works with truthy and falsey values:
It is also worth knowing that it converts all truthy and falsey values into false and true i.e. proper Boolean values. So !null is true, !0 is true and so on. So while:
is false. If you think about it what other interpretation of !"Hello" could you use in this case.
So Not converts all truthy and falsey values into Boolean values.
will flag the fact that A doesn't exist.
Of course there is no lazy evaluation with a NOT as it has only one operand and that has to be evaluated to work out its truth value.
In terms of a truth table this too is just a logical function but one with three inputs:
In this case it is the "active" interpretation of the logic that makes more sense than the "passive" truth table.
We have already noted that the AND && and OR || operators are the equivalent of if statements. That is:
are the same as
It is interesting to notice that the ternary expression is also the equivalent of an if..else statement. That is:
is the same as
This also means that the if and the if..else statements are just logical functions with the truth table given earlier.
This is not the way we usually think about them.
|Last Updated ( Thursday, 13 June 2019 )|