Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Checking if it's Done (Comparisons)

How does a chef know if the oven is at the right temperature, or if a dish has cooked long enough? They ask a question by comparing two values.

In Module 2, you learned about the boolean data type, which can only be true or false. You'll rarely type true or false directly. Instead, they are the answers your program gets from asking a question.

Think of a comparison operator as the question itself. For example, to ask "is the oven temperature greater than 400 degrees Fahrenheit?", you use the > operator. The computer will evaluate this question and answer with either true or false.

Here are the most common operators:

OperatorMeaningExampleResult
==Is equal to?5 == 5TRUE
!=Is not equal to?5 != 6TRUE
>Is greater than?5 > 4TRUE
<Is less than?5 < 4FALSE
>=Is greater than or equal to?5 >= 5TRUE
<=Is less than or equal to?5 <= 4FALSE

"==" not "="

A very common mistake is using a single equals sign = (which assigns a value) instead of a double == (which asks if values are equal).

Let's see it in action. The code below doesn't just print the number 375; it prints the answer to the question "is the oven_temperature variable greater than or equal to 350?"

oven_temperature = 375

# The computer evaluates the comparison (375 >= 350) and gets `True`.
# Then it prints that result.
print(oven_temperature >= 350)
let ovenTemperature = 375;

// The computer evaluates the comparison (375 >= 350) and gets `true`.
// Then it logs that result.
console.log(ovenTemperature >= 350);

Try changing the value of oven_temperature and see how the output changes from true to false.

Mini-Exercise 💡

  1. Create two number variables, a and b. Assign them any two different numbers.
  2. Write code to check if a is greater than b and print the result.
  3. Write code to check if a is equal to b and print the result.
  4. Write code to check if a is not equal to b and print the result.