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 backTwo 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
An online shop offers free shipping on orders of 1500 or more. Write an
ifstatement that printsFree shippingiforderTotalis greater than or equal to1500. - 2
Orders abroad take longer. Below it, write an
ifstatement that printsDelivery in 5 daysifcountryis not equal to"Czechia".