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)
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
: instead of the
if(prop("Type") == "Mammal", true, false)
If-then statements can also be written in a shorthand syntax. This uses
: as shorthand for
- Everything left of the
?is the statement being evaluated.
: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]
Here’s our example formula from above, re-written using shorthand:
prop("Type") == "Mammal" ? true : false
: 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
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:
Nested If-Then Statements
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", "Adult" ) )
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 */ )