Skip to content

Operators

Operators cover math, comparison, branching, and membership tests. The rules are close to JavaScript, but == is strict in Bonsai - there is no loose equality.

If you want to...UseExample
Do math+ - * / % **subtotal + shippingFee
Compare values== != < > <= >=order.total >= freeShippingThreshold
Branch or provide defaults?:, ??, ||, &&customer.nickname ?? customer.firstName
Test membershipin, not in"pro" in plans

Arithmetic

ExpressionResultContext / Note
basePrice + addOnPrice * seats85{ basePrice: 49, addOnPrice: 12, seats: 3 } (* runs before +)
2 ** 101024exponentiation

Comparison & equality

ExpressionResultContext / Note
order.total >= minimumOrdertrue{ order: { total: 149 }, minimumOrder: 100 }
1 == "1"falsestrict - no type coercion, ever

Conditional logic

Use ?? for nullish defaults, || for falsy defaults, and the ternary operator when you need an explicit if/else branch.

ExpressionResultNote
order.total >= freeShippingThreshold ? "free" : "paid""free"return a shipping label directly from the rule
customer.nickname ?? customer.firstName"Alicia"?? falls back only for null or undefined
couponCode || "no-code""no-code"|| also falls back for empty strings and other falsy values

Membership

in and not in work with arrays and strings. They are a good fit for tags, roles, lists, and substring checks.

ExpressionResultContext / Note
"pro" in planstrue{ plans: ["starter", "pro", "enterprise"] }
"invoice-" in fileNametruealso works as a substring check on strings

More precedence and edge cases

ExpressionResultNote
(subtotal + shippingFee) * taxMultiplier129.6parentheses make billing logic explicit
remainingSeats % seatsPerRow2remainder is useful for layout and batching rules
order.paid && order.fulfilledtrue
!user.suspendedtrueboolean negation
nickname ?? "Guest"""?? preserves empty strings and other defined falsy values
"legacy" not in enabledFeaturestrue

Guideline: when an expression becomes important business logic, add parentheses even when precedence would make it unnecessary. Stored rules are easier to review when grouping is explicit.