HTML Academy
Comparisons permitting equality
Conditions5/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?”
Strict string comparison
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Equality, inequality

And now we fixed the second version of the analyzer. But the boss heard somewhere about the theory of evaluation with the help of equalities and inequalities. Muffin needs the analyzer to compare the counts of markers of different colors according to the new rules, and the cat quickly executed this logic. And again, the rule “where the paws are fast, bugs are plenty” worked.

To fix this program, we need to go deeper into the comparison operators. To find out whether two values ​​are equal or not, JavaScript uses the ==, !=, === and !== operators.

OperatorNameAction
==Loose equality (with type conversion)Compares two values, converting types when needed. Returns true if the values compare equal.
===Strict equality (without casting types)Compares two values. If the value types are different or the values ​​are not equal, it returns false.
!=Inequality (with casting types)Compares two values using the same conversion rules as ==. Returns true if the values do not compare equal.
!==Strict inequality (without casting types)Compares two values. If the value types are different or the values ​​are not equal, it returns true.

What does “casts or does not cast values ​​to one type” mean? Let’s look at the examples.

console.log('123' == 123); // Logs true
console.log('123' != 123); // Logs false

The string and the number will be equal to each other. This is the case, because comparing different types with == converts values ​​to a single type. For example, you can get 123 from the '123' string. And the number 123 is equal to the number 123, that is why the comparison returns the true value. For the same reason, the inequality != returns ​false for these values: they can be brought to one type and then they will be equal to each other.

console.log('123' === 123); // Logs false
console.log('123' !== 123); // Logs true

In this case, the result is the opposite. Strict equality === compares the values without converting their types: here, a string and a number. Values of different types are never strictly equal, so the equality comparison returns false. But strict inequality for such values returns true.

Comments

  • script.js
JavaScript
var red = 1; var yellow = 2; var green = 3; var blue = 2; if (blue != yellow) { console.log('There are just as many blue markers as there are yellow ones. The forecast is moderately positive!'); } else { console.log('Wrong! The forecast is unacceptable!'); } if (blue == red) { console.log('The numbers of blue and red markers are different. The forecast is positive!'); } else { console.log('Wrong! The forecast is unacceptable!'); }

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 correct the conditions and experiment a little.

  1. Replace != with == in the first condition.
  2. Replace == with != in the second condition.
  3. Set the variable blue to the string value '2' instead of the number 2 and watch the result. Surprisingly, the comparisons will have the same results as before.
  4. Now in the first if, replace == with the strict equality operator ===. This time, the first condition should be false.

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.