HTML Academy
Comparisons permitting equality
Conditions5/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?”
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. The Muffin needs for the analyzer to compare the markers of the same color 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
==Approximate equality (with casting types)Compares two values, before this casts one of the values ​​to the type of the other. If the values ​​are equal, it returns true.
===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, before this casts one of the values ​​to the type of the other. If the values ​​are not equal, it returns true.
!==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); // Returns true
console.log('123' != 123); // Returns 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 value: they can be brought to one type and then they will be equal to each other.

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

In this case, the result is the opposite. Strict equality === does not lead to values ​​of the same type, but compares them as is: a string and a number. A string, whatever value it contains, is not equal to a number, that is why a comparison for equality returns false. But a 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('There are not as many blue markers as there are red ones. 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 != to == in the first condition.
  2. Replace == to!= in the second condition.
  3. Set the variable blue to the string value '2' instead of a number one and watch the result. Surprisingly, the conditions will work as before.
  4. Now in the first if, replace == with the operator of strict correspondence ===. This time, the first condition must not be fulfilled.

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.