if

Learn how to use the "if" function in Notion formulas.

The `if()` function allows you to write if-then statements within a Notion formula.

``````if([condition], ['then' expression], ['else' expression])

/* Argument 1 must always return a Boolean value.
In Formulas 1.0, arguments 2 and 3 needed to have a matching type. */
if(Boolean, string, string)
if(Boolean, number, number)
if(Boolean, Boolean, Boolean)
if(Boolean, date, date)

/* In Formulas 2.0, arguments 2 and 3 can have different data types. */
if(Boolean, string, number)
```Code language: JavaScript (javascript)```

If-then statements, also called conditional statements, contain:

• The condition – a statement to be evaluated for truthiness (i.e. “is it true?”)
• The “then” statement – a statement that is executed if the condition is true
• The “else” statement – a statement that is executed if the condition is false

`if()` is classed as a function in Notion, but can also be considered an operator as an if-then statement can be written with `?` and `:` instead of the `if()` syntax.

This formula compares the output of a Select property called Type (which has a data type of String) with the string value “Mammal”. If the two are equal, it outputs true; otherwise, false.

``````if(prop("Type") == "Mammal", true, false)
```Code language: JavaScript (javascript)```

Shorthand Syntax

If-then statements can also be written in a shorthand syntax. This uses `?` and `:` as shorthand for `if()`.

• Everything left of the `?` is the statement being evaluated.
• Between `?` and `:` is the output if the evaluated statement is true.
• Right of the `:` is the output if the evaluated statement is false.
``````[condition] ? ['then' statement] : ['else' statement]
```Code language: JavaScript (javascript)```

Here’s our example formula from above, re-written using shorthand:

``````prop("Type") == "Mammal" ? true : false
```Code language: JavaScript (javascript)```

Taken together, `?` and `:` form what’s called the conditional (or ternary) operator. It’s the only operator that takes in three operands, which are objects that are being operated on.

Most operators only work with two operands – for example:

• `2 + 5` – the add (`+`) operator is working on `2` and `5`

Check out the two formula properties in this example database; one uses the normal if-then syntax, while the other uses shorthand. You’ll see that their output is the same.

Other formula components used in this example:

With the introduction of the ifs function, Notion now supports else if statements, similar to those found in more popular scripting and programming languages such as Javascript.

With an else if statement, you could write code like this (in Javascript):

``````/* This is Javascript code that won't work in a Notion formula */

if (x<13) {
/* do one thing */
} else if (x<19) {
/* do another thing */
} else {
/* do the last thing */
}
```Code language: JavaScript (javascript)```

In Formulas 1.0, you would have to create nested if-then statements like so:

``````if(
prop("Age") < 13,
"Child",
if(
prop("Age") < 19,
"Teenager",
)
)```Code language: JavaScript (javascript)```

While nesting `if` statements as above is still supported, the introduction of the `ifs` function makes life much easier:

``````ifs(
prop("Age") < 13,
"Child", /* Do one thing */
prop("Age") < 19,
"Teenager", /* Do another thing */
"Adult" /* Do the last thing */
)```Code language: JavaScript (javascript)```