Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Comparison Operators

The symbols < and > that you've used in conditions are comparison operators: they compare two values, and the result is either true or false. JavaScript has six of them:

  • === equal to: 5 === 5
  • !== not equal to: 5 !== 3
  • > greater than: 5 > 3
  • < less than: 3 < 5
  • >= greater than or equal to: 5 >= 5
  • <= less than or equal to: 3 <= 5

All the examples in the list are true. In >= and <=, the equals sign always comes second, with no space in between. Watch the difference between > and >= at the boundary: 18 > 18 is false, but 18 >= 18 is true. “18 or older” is therefore age >= 18.

“Equal” is written with three equals signs, ===, and “not equal” with an exclamation mark and two equals signs, !==. Why not just one =? You'll find out in the next exercise.

You can compare text as well as numbers:

const password = "kitten42";
if (password === "kitten42") {
    console.log("Welcome back");
} else {
    console.log("Wrong password");
}

This prints:

Welcome back

Two texts are equal only if they match exactly, character by character: "Kitten42" with a capital K is a different text, and so is "kitten42 " with a space at the end. Don't forget the quotes around the text you compare with. Without them, password === kitten42 makes JavaScript look for a variable called kitten42, and the program ends with ReferenceError: kitten42 is not defined.

Instructions

  1. 1

    An online shop offers free shipping on orders of 1500 or more. Write an if statement that prints Free shipping if orderTotal is greater than or equal to 1500.

  2. 2

    Orders abroad take longer. Below it, write an if statement that prints Delivery in 5 days if country is not equal to "Czechia".

Back

3/13

  1. 1. The if Statement
  2. 2. The else Branch
  3. 3. Comparison Operators
  4. 4. Assign or Compare?
  5. 5. Quiz: Assign or Compare?
  6. 6. Boolean Values
  7. 7. More Options: else if
  8. 8. Logical Operators: And
  9. 9. Logical Operators: Or
  10. 10. Quiz: Logical Operators
  11. 11. Negation
  12. 12. Nested Conditions
  13. 13. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.