New in Formulas 2.0
The ifs()
function allows you to write if-then and else-if statements within a Notion formula. When used in its most basic form, ifs
behaves in the same way as if
, though it is a much simpler alternative to multiple nested ifstatements.
ifs(
[condition],
['then' expression],
[condition],
['then' expression],
[...]
['else' expression]
)
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
else-if Statements
Previously, Notion did not support the else-if statement that is available 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)
The ifs()
function allows you to write these else-if statements in a Notion formula, instead of relying on nested if
statements.
ifs(
[condition],
['then' expression], /* Do one thing */
[condition],
['then' expression], /* Do another thing */
['else' expression] /* Do the last thing */
)
Code language: JavaScript (javascript)
In Formulas 1.0, you would have to instead create nested if-then statements, like so:
if(
prop("Age") < 13,
"Child",
if(
prop("Age") < 19,
"Teenager",
"Adult"
)
)
Code language: JavaScript (javascript)
But now with the introduction of the ifs
function, the above is 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)
Example Formulas
Simple String Comparison
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.
ifs(
prop("Type") == "Mammal",
true,
false
)
Code language: JavaScript (javascript)
Multiple else-if
ifs(
/* Check if Done is checked, and if so display "😀" */
prop("Done"),
"😀",
/* Otherwise check if Due is empty, and if so display "⚪" */
not prop("Due"),
"⚪",
/* Otherwise, check if Due is the same as the current date,
and if so display "🟢" */
formatDate(prop("Due"), "L") == formatDate(now(), "L"),
"🟢",
/* Otherwise, check if the current date is past Due,
and if so display "🔴" */
prop("Due") < now(),
"🔴",
/* Otherwise, display "🔵" */
"🔵"
)
Code language: JavaScript (javascript)
Only Return a Value for a true Condition
You can also use ifs
to simply return a value if the first condition is true, with no need to add the “else” expression — this will be blank by default.
ifs(
/* Check if the current date is past Due Date */
now() > prop("Due Date"),
/* If it is, display this. Otherwise, display nothing. */
"This is overdue!"
)
Code language: JavaScript (javascript)