Skip to content

if - Conditionally return a value

The if function is useful for returning a value, if a given condition is true, otherwise returning some default value.

Syntax

if(condition: bool, then: any, else: any?): any

Arguments

Name Type Required Description
condition bool true Condition to be evaluated. Must return a bool
then any true Value if condition is true
else any false Value if condition is false

Example - Adding a flag based on IP subnet

Consider the following document:

{
    "ip": "10.8.0.8"
}

We're interested if a given IP address is in the 10.0.0.0/8 range. If it is, we don't want to keep calculating this, so we want to add a flag. We can do this using the if clause:

create is_in_10_subnet from if(ipInSubnet(ip, '10.0.0.0/8'), true, false)