The repeat()
function will repeat a string the specified number of times.
repeat(string, number)
string.repeat(number)
Code language: JavaScript (javascript)
If you specify a non-string data type as the first argument, it will be converted to a string before being repeated.
123.repeat(3) /* Output: "123123123" */
false.repeat(3) /* Output: "falsefalsefalse" */
Code language: CSS (css)
Example Formulas
"This is " + "very ".repeat(3).split(" ").join(", ") + " good."
Code language: JavaScript (javascript)
For Loops with repeat()
The repeat()
function has a clever trick up its sleeve – you can use it to essentially create for
loops within Notion formulas!
Notion’s formula language doesn’t include actual looping functions, unlike most programming languages. For example, JavaScript makes it easy:
const arr = []
for (let i = 0; i < 5; i++) {
const number = i * 2;
arr.push(number);
}
Code language: JavaScript (javascript)
However, you can combine repeat()
with split to replicate this functionality:
"."
.repeat(10)
.split(".")
.map(
index * 2
)
Code language: JavaScript (javascript)
This is about the simplest demonstration of this trick.
A .
character is repeated 10 times to create the string “……….”, which is then split on the same character to create a List of length 10. Finally, we map through the list, multiplying each item’s index by 2.
You can see a more in-depth example at my For Loops with Custom Number of Iterations example formula page.