Operators
This section describes the FQL operators. See Operator precedence for the operator precedence and associativity table.
Assignment
Operator | Syntax | Description |
---|---|---|
|
variable [:type] |
Simple assignment operator assigns the value to the declared variable. The optional :type notation constrains a value to the given type, where
type is one of the supported Types. For example, |
Arithmetic
The arithmetic operators perform arithmetic operations on numeric operands.
Operator | Syntax | Description |
---|---|---|
|
operand1 |
Addition, sums the operands. |
|
operand1 |
Subtraction, subtracts operand2 from operand1. |
|
operand1 |
Multiplication, multiplies the operands. |
|
operand1 |
Division, divides operand1 by operand2. |
|
operand1 |
Modulo, returns the remainder of operand1 divided by operand2 and takes the sign of the dividend. |
|
operand1 |
Exponentiation, returns the result of raising operand1 to the power of operand2. |
Concatenation
The plus operator performs concatenation on sting operands.
Operator | Syntax | Description |
---|---|---|
|
operand1 |
For String operands, concatenates the operands, left-to-right. |
Comparison
Comparison operators return a Boolean value.
For the <
, >
, ⇐
, and >=
operators, comparison across types always
returns false
and comparing non-comparable objects always returns false
.
Operator | Syntax | Description |
---|---|---|
|
expression1 |
Equal to. Returns
|
|
expression1 |
Not equal to. Returns |
|
expression1 |
Greater than. Returns
|
|
expression1 |
Greater than or equal to. Returns
|
|
expression1 |
Less than. Returns
|
|
expression1 |
Less than or equal to. Returns
|
|
expression1 |
Evaluate if expression1 is of type <type>, which can be any of the
supported runtime Types. The <type> must
be a module object. |
Logical
Logical operators return a Boolean value.
Operator | Syntax | Description |
---|---|---|
|
operand1 |
Logical OR. Returns |
|
operand1 |
Logical AND. Returns |
Bitwise operators
Operator | Syntax | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
expression1 | expression2 |
Bitwise inclusive OR:
|
|||||||||||||||
|
expression1 ^ expression2 |
Bitwise exclusive OR (XOR):
|
|||||||||||||||
|
expression1 & expression2 |
Bitwise AND:
|
Unary
Operator | Syntax | Description |
---|---|---|
|
|
Unary negation. Negates the operand it precedes. |
|
|
Logical negation (NOT). Inverts the value of the Boolean operand and returns a Boolean value. |
Examples:
Unary negation variations:
-5 // -5; A negative literal, not unary negation.
- 5 // -5; Unary negation applied to a literal.
let num = 5
-num // -5; Unary negation of a variable.
Insert a space between -
and operand if operand is a literal
Number.
Logical NOT:
!false // true
Optional chaining
Operator | Syntax | Description |
---|---|---|
|
object object |
Optional chaining. When accessing a field or invoking a method, if the
left side of the expression evaluates to |
Examples:
let book = {
name: 'Hamlet',
author: {
name: 'Shakespeare'
}
}
book.writer?.name
The example returns null instead of an error provided type checking is disabled.
The optional chaining operator can also be used with methods:
let book = {
name: 'Hamlet',
author: {
name: 'Shakespeare'
}
}
book.author.customMethod?.()
The example returns null instead of an error provided type checking is disabled.
Null coalescing
Operator | Syntax | Description |
---|---|---|
|
expression1 |
Null coalescing. If expression1 evaluates to |
Example with type checking disabled:
let book = {
name: 'Hamlet',
author: {
name: 'Shakespeare'
}
}
book.date ?? "Not found" // returns "Not found"
If type checking is enabled, the example returns an error.
Example with type checking enabled:
({ bar: null }).bar ?? "Not found" // returns "Not found"
Non-null assertion postfix
Operator | Syntax | Description |
---|---|---|
|
expression |
Non-null assertion postfix. Runtime validation: if expression evaluates to
|
Example:
let book = {
name: 'Hamlet',
author: {
name: 'Shakespeare'
}
}
book.date! // returns "Error: null_value" instead of null
If type checking is enabled, this example returns an error, instead.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!