Skip to content

Comparison Operators

Overview

Comparison operators allow you to compare values and create boolean expressions. These operators form the foundation of filtering and querying capabilities in the expression language.

Available Operators

Equality Operators

Equal To (==)

  • Compares if two values are exactly equal
  • Works with strings, numbers, and booleans
  • Case-sensitive for string comparisons

Examples:

js
status == "active"
age == 25
isEnabled == true
price == 19.99

Not Equal To (!=)

  • Compares if two values are different
  • Inverse of the equal operator
  • Works with strings, numbers, and booleans

Examples:

js
status != "inactive"
age != 0
isEnabled != false
price != 0.00

Numeric Comparison Operators

Greater Than (>)

  • Checks if the left value is greater than the right value
  • Works with integers and floating-point numbers
  • Can compare dates and timestamps when using appropriate fields

Examples:

js
age > 18
price > 100.00
timestamp > "2023-01-01"
count > 0

Less Than (<)

  • Checks if the left value is less than the right value
  • Works with integers and floating-point numbers
  • Can compare dates and timestamps when using appropriate fields

Examples:

js
temperature < 32
price < 50.00
timestamp < "2024-01-01"
remaining < 100

Greater Than or Equal To (>=)

  • Checks if the left value is greater than or equal to the right value
  • Combines greater than and equality checks
  • Works with numbers and comparable values

Examples:

js
age >= 21
price >= 0.00
score >= 75
quantity >= 1

Less Than or Equal To (<=)

  • Checks if the left value is less than or equal to the right value
  • Combines less than and equality checks
  • Works with numbers and comparable values

Examples:

js
temperature <= 100
price <= 999.99
grade <= 100
stock <= maxCapacity

Type Handling

Numeric Comparisons

  • Automatic type conversion between integers and floating-point numbers
  • Both operands must be numeric types
  • Mixing numeric types is allowed (e.g., comparing int with float)
js
// Valid comparisons
42 > 41.5
price <= 99.99
count >= 0

// Invalid comparisons
"42" > 41    // String with number
true > 1     // Boolean with number

String Comparisons

  • Only equality operators (==, !=) support direct string comparison
  • Case-sensitive comparisons
  • String literals can use single or double quotes
js
// Valid string comparisons
name == "John"
status != 'inactive'

// For other string operations, use dedicated functions
name starts "J"
description includes "important"
email ends "@example.com"

Boolean Comparisons

  • Only equality operators (==, !=) support boolean values
  • Boolean literals must be true or false
js
// Valid boolean comparisons
isActive == true
isDeleted != false

// Invalid boolean comparisons
isEnabled > true    // Cannot use ordering operators with booleans

Complex Comparisons

Combining with Logical Operators

Comparison operators can be combined using logical operators to create complex conditions:

js
// Using AND
age >= 18 and status == "active"

// Using OR
temperature < 0 or temperature > 100

// Using NOT
not (price >= 1000)

// Complex combinations
(age >= 18 and status == "active") or role == "admin"

Field Access in Comparisons

Comparisons can involve nested field access:

js
user.age >= 18
order.items[0].price < 100
metadata.tags.count > 0
settings.preferences.theme == "dark"

Function Results in Comparisons

When using functions that return numeric or string values, they must be used with comparison operators:

js
@length(items) > 0
@count(users) >= 10
@toLowerCase(name) == "admin"

Best Practices

  1. Type Safety

    • Always ensure compared values are of compatible types
    • Use appropriate operators for the data type
    • Handle potential type conversion errors
  2. Numeric Comparisons

    • Be aware of floating-point precision
    • Consider using >= or <= instead of == for floating-point comparisons
    • Handle potential overflow/underflow cases
  3. String Comparisons

    • Use case-sensitive comparisons mindfully
    • Consider using string operations (starts, ends, includes) for partial matches
    • Handle empty string cases explicitly
  4. Boolean Comparisons

    • Use only equality operators with boolean values
    • Consider logical operators for complex boolean expressions
    • Be explicit about boolean conditions
  5. Complex Expressions

    • Use parentheses to make precedence clear
    • Break down complex conditions into smaller parts
    • Consider readability and maintainability

Common Pitfalls

  1. Type Mismatches

    js
    // Invalid - mixing types
    "123" > 123
    true > 1
  2. Floating-Point Equality

    js
    // May not work as expected due to floating-point precision
    price == 19.99
    
    // Better approach
    price >= 19.98 and price <= 20.00
  3. Case Sensitivity

    js
    // These are different
    status == "Active"
    status == "active"
  4. Null/Undefined Values

    js
    // Handle potential null values
    user.age > 18  # Could fail if user is null