Skip to Content
Learn
Booleans and Comparison Operators
Comparison Operators

The condition, or expression, in an if statement can hold a boolean value—like TRUE or FALSE, a variable assigned to one of those values, or an expression that evaluates to TRUE or FALSE.

Just as we can represent a value of five with 5 or with an expression that evaluates to five e.g. 3 + 2, we can write expressions that evaluate to TRUE or FALSE. Comparison operators evaluate a relationship between two operands and return a boolean value.

The less than operator (<) will return TRUE if the left operand is less than the right operand and FALSE if it’s not:

1 < 10; // Evaluates to: TRUE 11 < 3; // Evaluates to: FALSE

The less than or equal to operator (<=) will return TRUE if the left operand is less than or equal to the right operand and FALSE if it’s not:

1 <= 10; // Evaluates to: TRUE 4 <= 4; // Evaluates to: TRUE 18 <= 2; // Evaluates to: FALSE

The greater than operator (>) will return TRUE if the left operand is greater than the right operand and FALSE if it’s not. And the greater than or equal to operator (>=) will return TRUE if the left operand is greater than or equal to the right operand and FALSE if it’s not:

1 > 10; // Evaluates to: FALSE 11 > 3; // Evaluates to: TRUE 1 >= 10; // Evaluates to: FALSE 11 >= 11; // Evaluates to: TRUE 54 >= 10; // Evaluates to: TRUE

Let’s practice writing conditionals using these operators!

Instructions

1.

Write a function chooseCheckoutLane(). This function should take in a single number argument representing the number of items a customer has. If the customer has 12 items or fewer, the function should return "express lane". Otherwise, the function should return "regular lane".

2.

In the U.S., citizens can vote if they are 18 years old or older. Write a function canIVote() that takes in a number representing an age, and return the string "yes" if they can vote, and the string "no" if they cannot.

3.

We tested your functions to make sure they work with various inputs, but you should be testing your own functions. Test each of your functions twice—once with an input that will enter the if block and once with an input that will enter the else block. Make sure to print the results to the terminal.

Folder Icon

Take this course for free

Already have an account?