View all results

Logical operators

FQL supports symbolic and keyword forms of the logical operators:

  • && or and
  • || or or
  • ! or not

The paired forms have the same behavior.

example.fql
read-only
return true && false return true or false return not false

Truth values for and and or

Binary and and or evaluate each operand according to its Boolean representation:

  • none is false.
  • Booleans keep their value.
  • Numeric zero is false; other numbers are true.
  • A zero Duration or DateTime is false; other temporal values are true.
  • An empty String is false; other strings are true.
  • Arrays and objects are true, even when empty.
  • Binary and host values are true.

The conversion is used only to decide control flow. and and or return one of their original operands rather than an automatically converted Boolean.

Logical and

and evaluates the left operand first. If it is false, the expression returns that operand without evaluating the right operand. Otherwise, it evaluates and returns the right operand.

example.fql Ferret v2
query.fql
FQL
let user = { active: true, name: "Ada" } return user.active && user.name

This returns "Ada" because the left operand is true.

example.fql
read-only
false && "value" // false none and true // NONE 0 && "fallback" // 0 true && 23 // 23

Logical or

or evaluates the left operand first. If it is true, the expression returns that operand without evaluating the right operand. Otherwise, it evaluates and returns the right operand.

example.fql Ferret v2
query.fql
FQL
let user = { displayName: "" } return user.displayName || "Anonymous"
example.fql
read-only
true || "value" // true 1 or 7 // 1 none || "fallback" // "fallback" "" || "fallback" // "fallback"
Use ?? when only none should select the fallback. Unlike or, none coalescing preserves false, zero, and empty strings.

See none-Coalescing Operator for the fallback semantics and examples.

Logical not

Unary ! and not accept only Boolean operands and always return a Boolean.

example.fql Ferret v2
query.fql
FQL
return { notTrue: !true, notFalse: not false }

Other types produce an operator-oriented runtime error:

example.text
read-only
operator '!' cannot be applied to String operator '!' cannot be applied to Int

Use to_bool(value) when explicit Boolean conversion is intended.

Double negation is no longer an implicit conversion mechanism. Replace expressions such as !!value with to_bool(value).

Short-circuit evaluation

The right side of and is evaluated only when the left side is true. The right side of or is evaluated only when the left side is false.

example.fql Ferret v2
query.fql
FQL
let user = { active: false, name: "Ada" } return user.active && user.name

Subqueries are an exception to this source-level model. Query planning may evaluate a subquery operand before the logical operator, so short-circuiting should not be used to suppress a subquery’s execution.

Result values

! and not always return a Boolean. and and or return an operand, so their result may have any FQL type.

example.fql
read-only
return 25 > 1 && 42 != 7 return 22 in [23, 42] || 23 not in [22, 7] return none || "fallback" return true && 23

When a strict Boolean result is required from a binary logical expression, pass the result to to_bool.

Next steps