comparison operators

When writing conditional statements, we need to be able to compare values.

We use comparison operators to achieve this:

less than <

Is 10 less than 20?

10 < 20

greater than >

Is 10 greater than 20?

10 > 20

less than or equal to <=

Is 10 less than or equal to 20?

10 <= 20

greater than or equal to >=

Is 10 greater than or equal to 20?

10 >= 20

is equal to ===

Is pizza equal to pie?

'pizza' === 'pie'

is not equal to !==

Is pizza not equal to pie?

'pizza' !== 'pie'

When using a comparison operator, the value on the left is compared to the value on the right. It can be helpful to think of comparison statements as questions. When the answer is “yes”, the statement evaluates to true, and when the answer is “no”, the statement evaluates to false. We can also use comparison operators on different data types, like strings.

All comparison statements evaluate to either true or false and are made up of:

  • Two values that will be compared.
  • An operator that separates the values and compares them accordingly (><<=,>=,===,!==).