Skip to content
Gallery
Coda Formulas to JavaScript
Share
Explore

icon picker
Coda Formulas to JavaScript

Search Formula Name:
Formulas
Category
i
Coda Formula
i
Formula Documentation
JavaScript
Warning
Notes
Collections
8
Count(list...)
list.length

CountUnique(value...)
(new Set(value)).size

Find(needle, haystack)
haystack.indexOf(needle, startAt)

MaxBy(list, compareBy)
list.sort((a, b) =>
compareBy(a) > compareBy(b) ? 1 : -1)
[list.length + 1]

MinBy(list, compareBy)
list.sort((a, b) =>
compareBy(a) > compareBy(b) ? 1 : -1)
[0]

Slice(value, start)
value.slice(start, end)
End index non-inclusive.
Sort(dataset)
dataset.sort((a, b) =>
sortBy(a) > sortBy(b) ? 1 : -1)
JS sorts lexographically
Splice(value, start, deleteCount, insertValue...)
value.splice(start, deleteCount, insertValue)
To insert a list, use ...insertValue
Dates
32
Created(object)
You can’t pass entire rows into a Pack, so there is no equivalent.
CurrentTimezone()
Packs run on the server, and don’t have any information about the user looking at the doc.
Date(year, month, day)
new Date(year + "-" + month + "-" + day)

DateTime(year, month, day)
new Date(year + "-" + month + "-" + day)

DateTimeTruncate(dateOrTime, unit)

DateToEpoch(date)
date.getTime()

Day(dateTime)
dateTime.getDate()
Time zone shifting
DocumentTimezone()
context.timezone

EndOfMonth(dateTime, monthOffset)

EpochToDate(epochTime)
new Date(epochTime)

Hour(dateOrTime)
dateTime.getHours()
Time zone shifting
IsoWeekday(dateTime)
dateTime.getDay()

IsoWeekNumber(dateTime)

Minute(dateOrTime)
dateTime.getMinutes()

Modified(object)
You can’t pass entire rows into a Pack, so there is no equivalent.
Month(dateTime)
dateTime.getMonth()
Months start at 0.
MonthName(dateTime)
dateTime.toLocaleDateString("en", {
month: "long"
})
Time zone shifting
NetWorkingDays(startDate, endDate)

Now()
new Date()

RelativeDate(dateTime, months)
dateTime = dateTime.setMonth(
dateTime.getMonth() + months)

Second(dateOrTime)
dateTime.getSeconds()

Time(hour, minute, second)
JavaScript doesn’t have a native way to represent a pure time value.
TimeValue(time)
JavaScript doesn’t have a native way to represent a pure time value.
ToDate(text)
Date.parse(text)

ToDateTime(datetime)
Date.parse(text)

Today()
let today = new Date();
today.setHours(0, 0, 0, 0);

ToTime(value)
JavaScript doesn’t have a native way to represent a pure time value.
Weekday(dateTime)
dateTime.getDay()

WeekdayName(dateTime)
dateTime.toLocaleDateString("en", {
weekday: "long"
})

WeekNumber(dateTime)

Workday(startDate, numWorkingDays)

Year(dateTime)
dateTime.getFullYear()

Duration
9
Days(days)

Duration()

Hours(hours)

Minutes(minutes)

Seconds(seconds)

ToDays(duration)

ToHours(duration)

ToMinutes(duration)

ToSeconds(duration)

Filters
7
AverageIf(list, expression)
let filtered = list.filter(num => expression);
let result = filtered.reduce((sum, num) => sum + num, 0) / filtered.length;

CountIf(list, expression)
list.filter(num => expression).length

Filter(list, expression)
list.filter(currentValue => expression)

IsFromTable(row, table)
You can’t pass entire rows into a Pack, so there is no equivalent.
Lookup(table, column, match value)
You can’t pass entire rows or tables into a Pack, so there is no equivalent.
Matches(value, control)
The logic in the Matches() formula is complex, and there isn’t a simple equivalent in JavaScript.
SumIf(list, expression)
list.filter(num => expression)
.reduce((result, num) => result + num)

Logical
9
And(value...)
value1 && value2

False()
false

If(condition, ifTrue, ifFalse)
let result;
if (condition) {
result = ifTrue;
} else {
result = ifFalse;
}

IfBlank(value, ifBlank)
let result = value;
if (result == undefined) {
result = ifBlank;
}

Not(value)
!value

Or(value...)
value1 || value2 || value3

Switch(expression, value, result, arg...)
let result;
switch (expression) {
case value1:
result = result1;
break;
case value2:
result = result2;
break;
default:
result = defaultValue;
}

SwitchIf(condition, ifTrue, arg...)
let result;
if (condition1) {
result = ifTrue1;
} else if (condition2) {
result = ifTrue2;
} else {
result = defaultValue;
}

True()
true

Info
12
IsAnyText(value)
typeof value == "string"

IsBlank(value)
value == undefined

IsDate(value)
value instanceof Date

IsLogical(value)
typeof value == "boolean"

IsNotBlank(value)
value != undefined

IsNotText(value)
typeof value != "string"

IsNumber(value)
typeof value == "number"

IsPlainText(value)

IsRichText(value)

IsText(value)
typeof value == "string"

ToNumber(value)
parseFloat(value, base)

ToText(value)
value.toString()

Math
39
AbsoluteValue(number)
Math.abs(number)

Average(value...)
list.reduce((sum, num) => sum + num, 0) / list.length

BinomialCoefficient(n, k)

Ceiling(value)
Math.ceil(value)

Even(value)
value % 2 == 0 ? value : value + 1

Exponent(value)
Math.exp(value)

Factorial(value)

Floor(value)
Math.floor(value)

IsEven(value)
value % 2 == 0

IsOdd(value)
value % 2 == 1

Ln(number)
Math.log(number)

Log(number, base)
Math.log(number) / Math.log(base)

Log10(number)
Math.log10(number)

Max(value...)
Math.max(value1, value2)
Math.max(...list)

Median(value...)

Min(value...)
Math.min(value1, value2)
Math.min(...list)

Mode(value...)

Odd(value)
value % 2 == 1 ? value : value + 1

Percentile(dataset, percentile)

PercentileRank(dataset, value)

Pi()
Math.PI

Power(number, exponent)
Math.pow(number, exponent)

Product(value...)
value1 * value2

Quotient(dividend, divisor)
dividend / divisor

Random()
Math.random()

RandomInteger(low, high)
Math.floor(Math.random() * (high - low + 1) + low)

Rank(value, dataset)

Remainder(dividend, divisor)
dividend % divisor

Round(number)
Math.round(number)

RoundDown(number)
Math.floor(number)

RoundTo(value, factor)

RoundUp(number)
Math.ceil(number)

Sign(number)
number / Math.abs(number)

SquareRoot(number)
Math.sqrt(number)

StandardDeviation(value...)

StandardDeviationPopulation(value...)

Sum(value...)
value1 + value2

SumProduct(list1, list2)
let result = 0;
for (let i = 0; i < list1.length; i++) {
result += list1[i] * list2[i];
}

Truncate(number)

Misc
5
DocId(url)
url.match(/_d([\w-]+)/)?.[1]

ObjectLink(object)
You can’t pass entire rows into a Pack, so there is no equivalent.
PageName(object)

ParseCSV(csvString)

SwapDocIdInUrl(url, docId)

Object
1
ParseJSON(jsonString)
JSON.parse(jsonString).path.path
Instead of using a path string, “dot” into the parsed object.
People
4
CreatedBy(object)
You can’t pass entire rows into a Pack, so there is no equivalent.
IsSignedIn()
Packs run on the server, and don’t have any information about the user looking at the doc.
ModifiedBy(object)
You can’t pass entire rows into a Pack, so there is no equivalent.
User()
Packs run on the server, and don’t have any information about the user looking at the doc.
Relational
3
Let(value, name, expression)
let name = value;
// Do expression

RowId(row)
Packs run on the server, and don’t have any information about the user looking at the doc.
WithName(value, name, expression)
let name = value;
// Do expression

RichText
3
BulletedList(value...)
JavaScript doesn’t have a concept of rich text. You can however return or
.
IndentBy(text, levels)
JavaScript doesn’t have a concept of rich text. You can however return or
.
NumberedList(value...)
JavaScript doesn’t have a concept of rich text. You can however return or
.
Shape
6
ClipCircle(image)

Embed(url)
Not possible using pure JavaScript, but can be done in the .
Hyperlink(url)
Not possible using pure JavaScript, but can be done in the Pack SDK.
HyperlinkCard(url)
Not possible using pure JavaScript, but can be done in the Pack SDK.
Image(url)
Not possible using pure JavaScript, but can be done in the .
Rectangle(width)

Spatial
2
Distance(location1, location2)

Location(latitude, longitude)

String
30
Character(charNumber)
String.fromCharCode(charNumber)

Concatenate(text...)
[text1, text2, text3].join("")

ContainsText(text, searchText)
text.includes(searchText)
To ignore case, apply .toLowerCase() to both strings first.
DecodeFromBase64(base64Text)
Buffer.from(base64Text, "base64").toString()

EncodeAsBase64(text)
Buffer.from(text).toString("base64")

EncodeForUrl(text)
encodeURIComponent(text)

EndsWith(text, suffix)
text.endsWith(suffix)

Format(template, text...)
`Hi ${text1}, my ${text2} is ${text3}`
This is an example of a in JavaScript.
Join(delimiter, text...)
text.join(delimeter)

Left(text, numberOfCharacters)
text.substring(0, numberOfCharacters)

LeftPad(text, targetLength)
text.padStart(targetLength, padString)

Length(text)
text.length

LineBreak()
"\n"

Lower(text)
text.toLoewerCase()

Middle(text, start, numberOfCharacters)
text.substring(start, start + numberOfCharacters)

RegexExtract(text, regularExpression)
text.match(regularExpression)

RegexMatch(text, regularExpression)
text.match(regularExpression)
Regular expressions aren’t strings.
RegexReplace(text, regularExpression, replacementText)
text.replace(regularExpression, replacementText)
Need to use /g modifier to replace them all.
Repeat(text, repetitions)
text.repeat(repetitions)

Replace(text, start, numberOfCharacters, replacementText)
let chars = text.split("");
chars.splice(start, numberOfCharacters, replacementText);
text = chars.join("");
Remember that JavaScript indices start at zero (0).
Right(text, numberOfCharacters)
text.substring(text.length - numberOfCharacters)

RightPad(text, targetLength)
text.padEnd(targetLength, padText)

Split(text, delimiter)
text.split(delimiter)

StartsWith(text, prefix)
text.startsWith(prefix)

Substitute(text, searchFor, replacementText)
text.replace(searchFor, replacementText)

SubstituteAll(text, searchFor, replacementText)
text.replaceAll(searchFor, replacementText)

ToByteSize(byteCount)

ToHexadecimal(decimalNumber)
decimalNumber.toString(16)

Trim(text)
text.trim()

Upper(text)
text.toUpperCase()

Lists
20
All(list)
list.every(expression)

Any(list)
list.some(expression)

Contains(value, search...)
search.some(item => value.includes(item))
The code assumes that search is an array of values.

ContainsAll(value, search...)
search.every(item => value.includes(item))

ContainsOnly(value, search...)
value.length == search.length && search.every(item => value.includes(item))

CountAll(list)
list.length

Duplicates(value...)
[...new Set(value.filter((item, index, arr) => arr.indexOf(item) !== index))]

First(list)
list[0]
Arrays in JavaScript start at index zero (0).
ForEach(list, formula)
list.map(item => {
// Apply the mapping to item.
return item;
})
Same as
@FormulaMap(list, formula)
FormulaMap(list, formula)
list.map(item => {
// Apply the mapping to item.
return item;
})

In(search, value...)
value.includes(search)

Last(list)
list[list.length - 1]
Arrays in JavaScript start at index zero (0).
List(value...)
[value1, value2, value3]

ListCombine(value...)
value1.concat(value2, value3)

Nth(list, position)
list[position]
Arrays in JavaScript start at index zero (0).
RandomItem(list)
list[Math.floor(Math.random() * list.length)]

RandomSample(list, count)

ReverseList(list)
list.reverse()

Sequence(start, end)
let result = [];
for (let i = start; i <= end; i += by) {
result.push(i);
}

Unique(value...)
Array.from(new Set(value))


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.