Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Fgeneral%2Fif.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Fgeneral%2Fif.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# `if`

## Description

Return one value if a condition is `true`, otherwise return an alternative value.

## Syntax

Like many functions in DataPrime, `if` supports<!-- --> [two notations](https://docs-docusaurus.kinsta.page/dataprime/language-reference/functions-reference/.md),<!-- --> **function** and **method**. These interchangeable forms allow flexibility in how you structure expressions.

* Function notation
* Method notation

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

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

## Arguments

| Name        | Type   | Required  | Description                                 |
| ----------- | ------ | --------- | ------------------------------------------- |
| `condition` | `bool` | **true**  | Condition to evaluate                       |
| `then`      | `any`  | **true**  | Value to return if the condition is `true`  |
| `else`      | `any`  | **false** | Value to return if the condition is `false` |

## Example

**Use case: Add a flag based on whether an IP belongs to a subnet**

If an IP is in the `10.0.0.0/8` range, mark it with a flag. This avoids recalculating the condition multiple times.

### Example data

```
{

    "ip": "10.8.0.8"

}
```

### Example query

* Function notation
* Method notation

```
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)
```

### Example output

```
{

    "ip": "10.8.0.8",

    "is_in_10_subnet": true

}
```
