JavaScript / Making decisions

Comparison and logical operators

Write conditions such as “greater than” and “not equal”, and combine two conditions into one with && and ||.

== 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.

OperatorMeaningExample (x = 30)Result
==equal tox == 50false
!=not equal tox != 50true
>greater thanx > 20true
<less thanx < 20false
>=greater than or equal tox >= 30true
<=less than or equal tox <= 50true
JavaScript
let x = 30;
console.log(x > 20);
console.log(x < 20);
console.log(x >= 30);
console.log(x != 30);
Console
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:

JavaScript
let height = 91;
if (height > 130) {
    console.log("Can ride");
} else {
    console.log("Cannot ride");
}
Console
Cannot ride
In >= 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:

OperatorRead asThe condition is true when
&&andboth conditions are true
||orat least one condition is true
JavaScript
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");
}
Console
20% off
30% off

With && you can check that a value lies between two numbers:

JavaScript
let temperature = 25;
if (temperature >= 20 && temperature <= 30) {
    console.log("Nice weather");
} else {
    console.log("Not so nice");
}
Console
Nice weather
Write a full comparison on each side: age <= 12 || age >= 65, not age <= 12 || >= 65. The sign & is called an ampersand and | a vertical bar, and each is written twice.

Practice

1Fill in the blankCurriculum practice

Complete the condition: if word is greater than or equal to 70, print pass; otherwise print fail.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let word = 60;
if (___) {
    console.log("pass");
} else {
    console.log("fail");
}
Console
Press Run to see the result.
2Fill in the blankCurriculum practice

Complete the condition: if height is greater than 130, print Can ride; otherwise Cannot ride. (The value is 170.)

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
let height = 170;
if (___) {
    console.log("Can ride");
} else {
    console.log("Cannot ride");
}
Console
Press Run to see the result.
3Write the programCurriculum practice

Declare the variable age and assign it 15. If it is greater than or equal to 60, print Cannot jump; otherwise print Can jump.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
4Write the programCurriculum practice

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.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
5Write the programCurriculum practice

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.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
6Write the programExtra practice

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.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.
7Write the programExtra practice

Declare the variable day and assign it the text "Saturday". If the day is "Friday" or "Saturday", print Weekend; otherwise print School day.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Console
Press Run to see the result.

Key points

  • Comparison operators: == != > < >= <=; each comparison gives true or false.
  • && (and) is true when both conditions are true; || (or) when at least one is.
  • Write a full comparison on each side of && and ||.