HTML Academy
If the condition is not fulfilled
Conditions10/17
Back to the list of tasks
  • 1. A simple branch
  • 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 should I walk?”
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 checks 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 with different types of data.

The fact of the matter is that we receive information on vacations from managers in completely different formats. Some will immediately 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 string.

Here, the input may have different types. An if statement tests the condition’s value as a Boolean, so we can use numbers, strings, true, false, or variables holding those values directly as conditions.

The main thing is to understand how these values are converted to a Boolean. true and false remain themselves. Of the numbers used here, all except 0 are converted to true, and 0 is converted to false. All strings except the empty string are converted to true; the empty string '' is converted to false. A variable declared without assigning a value contains undefined, which is also converted to false.

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 Muffin’s new 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 with onVacation.
  2. Set 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 assignment from the declaration, leaving just var onVacation;.

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

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.