HTML Academy
If the condition is not fulfilled
Conditions10/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?”
Nested conditions
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Values ​​as a condition

Muffin began working on another program “Can I start the project?”. And we will test it and fix the errors, if necessary.

The program reviews several conditions and gives an expert opinion. First, it checks to see if any of the developers are on vacation. We need to test this condition for different types of data.

The fact of the matter is that we receive information on vacations from managers in completely different formats. Some immediately will say that the developers are still on vacation, that is, will give us true, and some will not understand the question and say how many developers are on vacation, that is, give us a number. And some will remain silent; if no one is on vacation, we will get an empty line.

Here the comparison of values ​​does not help us, because the data can come in any form. Since in the condition block all values ​​are converted to a logical type, we can use any values ​​as conditions: numbers, strings, true and false, variables that contain such data.

The main thing is to understand how these values ​​are cast to a logical type. If true and false remain themselves, numbers and strings are cast in a special way. For example, all numbers except 0 are true, and 0 is false. All lines except the empty string are true, empty string '' is false. You can say that values ​​that do not seem to contain anything (like 0 or empty string ''), are cast to false, and all the others are cast to true.

if ('some line') {
  // Non-empty string cast to true
  // Condition will be met
};

if ('') {
  // Empty string cast to false
  // Condition will not be met
};

if (123) {
  // Number is cast to true
  // Condition will be met
};

if (0) {
  // 0 is cast to false
  // Condition will not be met
};

Now we can return to the new Muffin’s program “Can I start the project?”. To verify that the condition is working correctly, we will try to change the value of the onVacation variable.

Comments

  • script.js
JavaScript
var onVacation = false; if (true) { console.log('You cannot start the project'); } else { console.log('You can start the project'); }

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 fix the program and experiment with the value of the variable onVacation.

  1. Replace the condition in if to onVacation.
  2. Replace the value of the onVacation variable to true.
  3. And now to 2. The number is not equal to zero and casts to true.
  4. And to 0. The message should change.
  5. Now to ''.
  6. And, finally, remove the value from the variable, leaving just var onVacation;.

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.