Use cases won't always require checking for a positive, or true, condition, which is where the NOT operator comes in. Written with a single exclamation point, the NOT operator allows for negative, or false, conditions to be met by if or else-if statements. This means that the following conditions are the same:
if(variable == false)
// AND
if(!variable)
As you already know, you can check for Boolean values, literal values, or expressions in an if condition. So, naturally, the NOT operator has to be adaptable. Take a look at the following example of two different negative values, hasDungeonKey and weaponType, used in an if statement:
We can evaluate each statement as follows:
- The first statement can be translated to, "If hasDungeonKey is false, the if statement evaluates to true and executes its code block."
If you're asking yourself how a false value can evaluate to true, think of it this way: the if statement is not checking whether the...