Java Reference
In-Depth Information
NOT
In English, we might say, “If I'm not hot, then I'll eat soup.” The condition being evaluated is whether
we're hot. The result is true or false, but in this example we act (eat soup) if the result is false.
However, JavaScript is used to executing code only if a condition is true. So if you want a false condi-
tion to cause code to execute, you need to switch that false value to true (and any true value to false).
That way you can trick JavaScript into executing code after a false condition.
You do this using the NOT operator. This operator reverses the logic of a result; it takes one Boolean value
and changes it to the other Boolean value. So it changes true to false and false to true. This is some-
times called negation .
To use the NOT operator, you put the condition you want reversed in parentheses and put the ! symbol
in front of the parentheses. For example:
if (!(degCent < 100))
{
// Some code
}
Any code within the braces will be executed only if the condition degCent < 100 is false.
The following table details the possible results when using NOT.
Right-Hand Side
Result
true
false
false
true
Multiple Conditions Inside an if Statement
The previous section started by asking how you could use the condition “Is degFahren greater than
zero but less than 100?” One way of doing this would be to use two if statements, one nested inside
another. Nested simply means that there is an outer if statement, and inside this an inner if statement.
If the condition for the outer if statement is true, then (and only then) the nested inner if statement's
condition will be tested.
Using nested if statements, your code would be:
if (degCent < 100)
{
if (degCent > 0)
{
document.write(“degCent is between 0 and 100”);
}
}
Search WWH ::




Custom Search