The lets()
function allows you to created multiple named variables that can store values and be used within expressions.
lets(variable, value, variable, value, ..., expression)
variable.lets(value, variable, value, ..., expression)
Code language: JavaScript (javascript)
lets()
is one of the most useful functions in Notion’s formula language. It works similarly to let, but it allows you to define multiple named variables instead of just one.
Hint: If you’re defining multiple variables and you hit an error, check to see that you’re using lets()
rather than let()
. This is a common mistake, especially when you start writing a formula assuming you’ll only need one variable, then find you need to add more later on.
A few of its primary uses:
- Define a variable with complex logic, then reference it in multiple places throughout a formula
- Separate formula logic from display (e.g. using variables when constructing final string values)
- Access the
current
variable of an outer List function within an inner List function (e.g.map(filter())
Example Formulas
lets(
firstName, "Monkey",
middleName, "D.",
lastName, "Luffy",
firstName + " " + middleName + " " + lastName
)
/* Output: "Monkey D. Luffy" */
lets(
weekendFlag,
if(
today().day() > 5,
"is",
"is not"
),
monthLabel,
today().formatDate("MMMM"),
"It is " + monthLabel + ", and it currently " + weekendFlag + " the weekend."
)
/* Output: "It is December, and it currently is not the weekend." */
Code language: JavaScript (javascript)
Note that you can use lets()
inside of another instance of let()
or lets()
. However, variables can only be used inside of the enclosing parentheses of the lets()
instance where they were defined.
In other words, you can use variables from an outer lets()
within an inner lets()
– but not the other way around.
lets(
var1, 2 + 2,
var2, 3 + 3,
var3, lets(
var4, var1 * var2,
var5, var4 / var1,
var5
),
var3 - var1
)
Code language: JavaScript (javascript)
It’s worth pointing out that the value of var3
in the above example is returned by the inner lets()
!
Another extremely useful trick you can do with let()
and lets()
is accessing the current
value of an outer List function inside of an inner List
function, which has its own current
value.
Consider the formula below. The goal is to filter a List of Lists (e.g. a multi-dimensional array), returning only the elements that pass the following test: If you sum up all the numbers in the list, then divide that sum by each number in the list, every quotient has no remainder.
To do this, we need to use filter() to produce a filtered list. However, we also need to test every item within each inner list. To do this, we can use lets()
inside of the filter()
call. Within lets()
, we can store the value of current
(in the context of filter()
), in a variable called currentList
.
This allows us to reference the current List inside of the inner map() call, which has its own current
keyword that represents the individual number value from the inner List being passed into map()
.
[
[1, 3, 5, 7],
[2, 4, 6, 8],
[1, 2, 3, 6]
].filter(
lets(
currentList, current,
remainders, current.map(
currentList.sum() % current
),
remainders.every(current == 0)
)
)
Code language: JavaScript (javascript)
Example Database
This example database shows how you can display an HH:MM:SS
duration based on two Date properties that represent the start and end of a session.
View and Duplicate Database
“Duration” Property Formula
lets(
hoursCount, dateBetween(prop("End"), prop("Start"), "hours"),
hoursShift, prop("Start").dateAdd(hoursCount, "hours"),
hoursLabel, [hoursCount < 10 ? "0" : "", hoursCount],
minutesCount, dateBetween(prop("End"), hoursShift, "minutes"),
minutesLabel, [minutesCount < 10 ? "0" : "", minutesCount],
[hoursLabel.join(""), minutesLabel.join("")].join(":") + ":00"
)
Code language: JavaScript (javascript)
Just as in the examples shown above, this formula defines several variables which hold the output of some complex logic.
Note how earlier variables are used within the definition of later variables. For example, hoursCount
is used to help define the values of hoursShift
and hoursLabel
.