HTML Academy
Nested conditions
Conditions12/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?”
Logic traps
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Logical operators: &&, ||

Work on the program is in full swing and the Muffin asks us to make several changes to the logic of decision-making:

  • The project can be started if there are enough developers and if they have the knowledge of the necessary technology.
  • The project cannot be started if one of the developers is on vacation or is on sick leave.

We already know how to implement the first part of the logic, the one that contains and, using nested conditions. But what about the second part: the one that includes or? We can combine conditions inside if using logical operators: && and ||. For example:

var conditionOne = true;
var conditionTwo = true;
var conditionThree = false;
var conditionFour = true;

if (conditionOne && conditionTwo) {
  // code will be executed
}
if (conditionThree || conditionFour) {
  // code will also be executed
}

The && operator or “logical AND” returns true only if both conditions, to the left and right of it, return true.

The operator || or “logical OR” returns true if any from the conditions to the left or to the right of it return true.

Now it is clear how to program the second part of the logic: let’s combine the conditions “employees on vacation” and “employees on sick leave” using “logical OR”.

We can also program the first part of the logic without nested if: let’s combine the conditions “there are enough developers” and “technology is mastered” using the “logical AND”. We decided not to nest the conditions as this could make the code complicated and confusing. If the nesting is large, then it becomes difficult to understand why this or that action is being performed.

Comments

  • script.js
JavaScript
var enoughDevelopers = true; var techAvailable = true; var onVacation = false; var onSickLeave = false;

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 describe the new logic of the program:

  1. Add the first condition that combines enoughDevelopers and techAvailable using the “logical AND”.
  2. Inside the condition, log the 'You can start the project' message in the console.
  3. Add below the second condition, combining onVacation and onSickLeave using “logical OR”.
  4. Inside the second condition, log the message 'It is better to wait'.
  5. Change the value of techAvailable variable to false, and the value of onSickLeave to true.

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.