== only asks “is it equal?”. Most decisions need other questions: is the mark 50 or more? Is the height more than 130? That is what comparison operators are for, and each one gives true or false.
| Operator | Meaning | Example (x = 30) | Result |
|---|---|---|---|
| == | equal to | x == 50 | false |
| != | not equal to | x != 50 | true |
| > | greater than | x > 20 | true |
| < | less than | x < 20 | false |
| >= | greater than or equal to | x >= 30 | true |
| <= | less than or equal to | x <= 50 | true |
let x = 30; console.log(x > 20); console.log(x < 20); console.log(x >= 30); console.log(x != 30);
true false true false
You put a comparison inside an if condition just as you did with ==. For example, a fairground ride only allows people taller than 130 cm:
let height = 91;
if (height > 130) {
console.log("Can ride");
} else {
console.log("Cannot ride");
}Cannot ride
>= and <= the = comes second, with no space. Writing => or > = is wrong. Mind the difference: x > 30 does not include 30, but x >= 30 does.Logical operators: && and ||
Sometimes a decision depends on two conditions. For example, a discount for people in a group and paying 3000 or more, or a cheaper ticket for children or older people. A logical operator joins the two conditions:
| Operator | Read as | The condition is true when |
|---|---|---|
| && | and | both conditions are true |
| || | or | at least one condition is true |
let numberOfPeople = 3;
let fee = 3500;
if (numberOfPeople >= 2 && fee >= 3000) {
console.log("20% off");
}
let age = 70;
if (age <= 12 || age >= 65) {
console.log("30% off");
}20% off 30% off
With && you can check that a value lies between two numbers:
let temperature = 25;
if (temperature >= 20 && temperature <= 30) {
console.log("Nice weather");
} else {
console.log("Not so nice");
}Nice weather
age <= 12 || age >= 65, not age <= 12 || >= 65. The sign & is called an ampersand and | a vertical bar, and each is written twice.Practice
Complete the condition: if word is greater than or equal to 70, print pass; otherwise print fail.
let word = 60;
if (___) {
console.log("pass");
} else {
console.log("fail");
}Press Run to see the result. Complete the condition: if height is greater than 130, print Can ride; otherwise Cannot ride. (The value is 170.)
let height = 170;
if (___) {
console.log("Can ride");
} else {
console.log("Cannot ride");
}Press Run to see the result. Declare the variable age and assign it 15. If it is greater than or equal to 60, print Cannot jump; otherwise print Can jump.
Press Run to see the result. Declare numberOfPeople with 2 and fee with 3500. If the number of people is 2 or more and the fee is 3000 or more, print Free mug.
Press Run to see the result. Declare the variable age and assign it 15. If the age is less than or equal to 15 or greater than or equal to 70, print Discounts available.
Press Run to see the result. To pass the year, a student needs a grade of 50 or more and attendance of 75 or more. Declare grade = 72 and attendance = 70. Print Passed if both are true, otherwise print Not passed.
Press Run to see the result. Declare the variable day and assign it the text "Saturday". If the day is "Friday" or "Saturday", print Weekend; otherwise print School day.
Press Run to see the result. Key points
- Comparison operators:
== != > < >= <=; each comparison givestrueorfalse. &&(and) is true when both conditions are true;||(or) when at least one is.- Write a full comparison on each side of
&&and||.