HTML Academy
Equality, inequality
Conditions6/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 number comparison
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Strict string comparison

What should you do if there are two values of different types, and you do not want to rely on automatic type conversion? You can convert them explicitly.

For example, you can cast a numeric value to a string type. One way is to use the number.toString() command. For example:

var number = 1;

// Logs number: 1 (number)
console.log(number);

// Logs string: "1" (string)
console.log(number.toString());

Comments

  • script.js
JavaScript
var string = '123'; var number = 123; if (string == number) { console.log('The values are equal!'); } else { console.log('The values are not equal.'); }

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 convert the second value to a string.

  1. Replace == with === in the condition. Strict equality does not convert types, so the condition will be false.
  2. Replace number in the condition with number.toString(). Both values are now strings, so the condition should be true.

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.