Comparison operators

This lesson won't have any Liquid code you can test out. It will be a general programming overview of the operators Liquid uses. I promise in the next lesson we will put what we learn here to use. 

Comparison operators are exactly that, comparing two values in different ways. The output of comparing values will be a boolean, true or false. For example:

10 > 3  // true
10 > 20  // false
10 >= 20  // false
10 < 20  // true
20 <= 20  // true
20 == 20  // true
20 != 20  // false

The operators we use are greater than >, greater than or equal to >=, less than <, less than or equal to <=, equal to ==, and does not equal to !=

Note: = is to assign values, == ,double equal signs, is to compare values.

Then we have the or operator. We have seen it used already in >= greater than or equal to. For the output to be true, one of the comparing values must be true. The or operator will only be false if both values are false. Here are the four possible outcomes. 

false or false // false
false or true // true
true or false // true
true or true // true

Looking how this is used in >= greater than or equal to, we can rewrite the operator the long way.

10 < 20 // false
10 == 20 // false
10 < 20 or 10 == 20 // false

20 < 20 // false
20 == 20 // true
20 < 20 or 20 == 20 // true

We see here how the or operator splits up >=. We also see we how we can combine comparisons, using < less than and equals to == statements on the same line. This can be done with all operators.

Finally, we have the and operator. We can use and the same way we use or. The output of and is as follows:

true and true // true
true and false // false
false and true // false
false and false // false

Order of operations

When using multiple and or statements, the operators are checked from right to left.

true or true and false // true
// First
true and false // false

// Second, using the output false
true or false // true

In the next lesson we will use these comparison operators in conditional statements.

Joe Pichardo | Shopify Developer

About Joe Pichardo

Joe Pichardo is a Shopify Developer creating themes and apps to help other programmers succeed on the ecommerce platform.