Comparison operators
Comparison operators are exactly that, comparing two values in different ways. The output of comparing values will be a boolean, true or false. The Liquid operators: greater than >, greater than or equal to >=,...
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 // true10 > 20 // false10 >= 20 // false10 < 20 // true20 <= 20 // true20 == 20 // true20 != 20 // falseThe 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 // falsefalse or true // truetrue or false // truetrue or true // trueLooking how this is used in >= greater than or equal to, we can rewrite the operator the long way.
10 < 20 // false10 == 20 // false10 < 20 or 10 == 20 // false
20 < 20 // false20 == 20 // true20 < 20 or 20 == 20 // trueWe 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 // truetrue and false // falsefalse and true // falsefalse and false // falseOrder of operations
When using multiple and or statements, the operators are checked from right to left.
true or true and false // true// Firsttrue and false // false
// Second, using the output falsetrue or false // trueIn the next lesson we will use these comparison operators in conditional statements.
Discussion
Loading comments...