HTML Academy
Values ​​as a condition
Conditions11/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?”
Logical operators: &&, ||
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Nested conditions

What if the decision does not depend on one, but on two or more conditions? You can use one if, and then, if the condition is fulfilled, execute one more condition inside the nested if:

if (condition1) {
  if (condition2) {
    actions;
  }
}

Then within the second check you can perform the third one and so on indefinitely.

Let’s continue writing the program “Can I start the project”, which became really complicated. Now you can proceed to the project if two conditions are met:

  1. there are enough developers: variable enoughDevelopers;
  2. and the developers have the knowledge of the required technologies: variable techAvailable.

If there are enough developers and if the required technology is mastered, we log the message “You can start the project”, otherwise the message remains unchanged: “You cannot start the project”.

Comments

  • script.js
JavaScript
var enoughDevelopers = true; var techAvailable = false; var message = 'You cannot start the project'; console.log(message);

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 a compound condition using nested if.

  1. Between declaring the message variable and logging in the console, add if with the enoughDevelopers condition.
  2. Inside the if block, add one more if with the techAvailable condition.
  3. Inside the block of the second if, add assignment message = 'You can start the project';.
  4. Change the value of the techAvailable variable to true. The value of message should change.

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.