HTML Academy
Logic traps
Conditions14/17
Back to the list of tasks
  • 1. Simple fork
  • 2. Let’s use an alternative
  • 3. Simple comparisons
  • 4. Comparisons permitting equality
  • 5. Equality, inequality
  • 6. Strict string comparison
  • 7. Strict number comparison
  • 8. Condition-based actions
  • 9. If the condition is not fulfilled
  • 10. Values ​​as a condition
  • 11. Nested conditions
  • 12. Logical operators: &&, ||
  • 13. Logic traps
  • 14. Logical negation
  • 15. Let’s combine logical operators
  • 16. Summary of “Conditions”
  • 17. Second program: “How long to take a walk for?”
Let’s combine logical operators
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Logical negation

Excellent! We found an error. As Descartes said: “If the bug is reproduced, then it exists”.

Where is the error? Now the program has two separate checks. And these checks can work simultaneously. When this happens, two messages appear.

To correct the error, let’s first introduce new checks with negation:

  • no developers are on vacation;
  • no developers are out sick.

In these conditions, the keyword is “no”, which means that they must be executed in cases where the value of the variable is  false and not executed if the value is true.

To create checks with negation, unary (single) logical operator ! is used. Example:

var condition = false;

if (!condition) {
  // code will be executed
}

Practice using the negation in the program “Can I start the project?”.

Comments

  • script.js
JavaScript
var enoughDevelopers = false; var techAvailable = false; var onVacation = false; var onSickLeave = false; if (enoughDevelopers && techAvailable) { console.log('You can start the project'); } if (onVacation) { console.log('No one is on vacation'); } else { console.log('Someone is on vacation'); } if (onSickLeave) { console.log('No one is on sick leave'); } else { console.log('Someone is on sick leave'); }

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted

Let’s experiment with negations.

  1. Change the vacation check to the check with negation.
  2. Do the same with the check about sick leave.
  3. Indicate that one of the developers is on vacation.
  4. Indicate that one of the developers is out sick.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.