HTML Academy
Equality, inequality
Conditions6/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 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 the default cast? Use can cast “by hand”.

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 comparison is working!'); } else { console.log('I cannot compare'); }

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 give the second value to the string type.

  1. Replace == with === in the condition. The cast will stop working and the condition will not be met.
  2. Replace number in the condition with number.toString(). The condition should start working.

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.