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

Like many functions in DataPrime, if supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

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

Arguments

NameTypeRequiredDescription
conditionbooltrueCondition to be evaluated. Must return a bool
thenanytrueValue if condition is true
elseanyfalseValue 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)
create is_in_10_subnet from ip.ipInSubnet('10.0.0.0/8').if(true, false)