Negation
Sometimes it is useful to negate a condition, i.e., change it from true to false or vice versa. For example, if we have a variable rain
and we want to execute a block of code when it is not raining, we would write it without negation as follows:
let rain = false;
if (rain === false) {
console.log("It is not raining");
}
Using negation (!
):
let rain = false;
if (!rain) {
console.log("It is not raining");
}
Now, this may not look very useful, but in certain cases, such as when you have many conditions linked by logical operators, it may be easier to think about some parts as negated.
Instructions
Use an if
statement to print I will go outside
if the internet does not work.
Start programming for free
7/9