Handlebar Helper Function: Compare

Compares two values using a specified operator. The operation supports both numeric and string comparisons, and automatically determines the appropriate comparison type.

ParameterTypeDescription
leftValueanyRequired. The left operand for comparison
operatorstringRequired. The comparison operator to use. Accepted values:
  • "==": Equal to
  • "!=": Not equal to
  • ">": Greater than
  • ">=": Greater than or equal to
  • "<": Less than
  • "<=": Less than or equal to
rightValueanyRequired. The right operand for comparison

The operation returns a boolean value (true or false) based on the result of the comparison.

The compare operation uses logical rules to evaluate the relationship between the leftValue and rightValue properties, according to the selected operator. It examines the types and values of both operands and determines the appropriate comparison strategy.

If the operator is == or !=, the operation follows these rules:

  1. If both values are null, they're considered equal and the operation returns true. If only one value is null, the operation returns false.
  2. If either value is a boolean, both are coerced using language truthiness rules and compared as booleans. For example, the boolean true is equal to the string "true".
  3. If either value is a date or datetime value, both are parsed and compared as dates.
  4. If either value is number-like, both are converted to numbers and compared numerically. For example, the string "5.0" is equal to the number 5.
  5. If either value is a string that can’t be converted to a number, the operation falls back to case-sensitive string comparison. For example, the string "Hello" isn’t equal to the string "hello".

If the operator is >, >=, <, or <=, the operation follows these rules:

  1. If either value is a number or is convertible to a number, the operation attempts to compare them numerically. For example, {{compare "5.5" ">" 3}} returns true.
  2. If either value is a date or datetime value, both are parsed and compared as dates.
  3. If either value is a string that can't be converted to a number or date, the operation performs an alphabetic, case-sensitive comparison. For example, {{compare "apple" "<" "banana"}} returns true.