Skip to content
Coda Formulas to JavaScript
Share
Explore

Coda Formulas to JavaScript

Search Formula Name:
Formulas
0
Search
Group
Coda Formula
Formula Documentation
JavaScript
Warning
Notes
Arrays
16
Contains(value, search)
search.some(item => value.includes(item))
The code assumes that search is an array of values.

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

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

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

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

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

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

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

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

Open
RandomSample(list, count, withReplacement, updateContinuously)
Needs library

Open
ReverseList(list)
list.reverse()

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

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

Open
Collections
8
Count(list)
list.length

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

Open
Find(needle, haystack, startAt, ignoreCase, ignoreAccents)
haystack.indexOf(needle, startAt)

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

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

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

Open
DateTimeTruncate(dateTime, unit)
Needs library

Open
DateToEpoch(date)
date.getTime()

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

Open
EndOfMonth(dateTime, monthOffset)
Needs library

Open
EpochToDate(epochTime)
new Date(epochTime)

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

Open
IsoWeekNumber(dateTime)
Needs library

Open
Minute(dateTime)
dateTime.getMinutes()

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

Open
Now(precision)
new Date()

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

Open
Second(dateTime)
dateTime.getSeconds()

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

Open
ToDateTime(datetime)
Date.parse(text)

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

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

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

Open
WeekNumber(dateTime, returnType)
Needs library

Open
Workday(startDate, numWorkingDays, holidays)
Needs library

Open
Year(dateTime)
dateTime.getFullYear()

Open
Duration
9
Days(days)
Needs library

Open
Duration(days, hours, minutes, seconds)
Needs library

Open
Hours(hours)
Needs library

Open
Minutes(minutes)
Needs library

Open
Seconds(seconds)
Needs library

Open
ToDays(duration)
Needs library

Open
ToHours(duration)
Needs library

Open
ToMinutes(duration)
Needs library

Open
ToSeconds(duration)
Needs library

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

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

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

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

Open
Logical
9
And(value)
value1 && value2

Open
False()
false

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

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

Open
Not(value)
!value

Open
Or(value1, value2, value3)
value1 || value2 || value3

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

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

Open
True()
true

Open
Info
9
IsBlank(value)
value == undefined

Open
IsDate(value)
value instanceof Date

Open
IsLogical(value)
typeof value == "boolean"

Open
IsNotBlank(value)
value != undefined

Open
IsNotText(value)
typeof value != "string"

Open
IsNumber(value)
typeof value == "number"

Open
IsText(value)
typeof value == "string"

Open
ToNumber(value, base)
parseFloat(value, base)

Open
ToText(value)
value.toString()

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

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

Open
BinomialCoefficient(n, k)
Needs library

Open
Ceiling(value, factor)
Math.ceil(value)

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

Open
Exponent(value)
Math.exp(value)

Open
Factorial(value)
Needs library

Open
Floor(value, factor)
Math.floor(value)

Open
IsEven(value)
value % 2 == 0

Open
IsOdd(value)
value % 2 == 1

Open
Ln(number)
Math.log(number)

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

Open
Log10(number)
Math.log10(number)

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

Open
Median(value)
Needs library

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

Open
Mode(value)
Needs library

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

Open
Percentile(dataset, percentile)
Needs library

Open
PercentileRank(dataset, value)
Needs library

Open
Pi()
Math.PI

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

Open
Product(value)
value1 * value2

Open
Quotient(dividend, divisor)
dividend / divisor

Open
Random(updateContinuously)
Math.random()

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

Open
Rank(value, dataset, ascending)
Needs library

Open
Remainder(dividend, divisor)
dividend % divisor

Open
Round(number, places)
Math.round(number)

Open
RoundDown(number, places)
Math.floor(number)

Open
RoundTo(value, factor)
Needs library

Open
RoundUp(number, places)
Math.ceil(number)

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

Open
SquareRoot(number)
Math.sqrt(number)

Open
StandardDeviation(value)
Needs library

Open
StandardDeviationPopulation(value)
Needs library

Open
Sum(value)
value1 + value2

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

Open
Truncate(number, places)
Needs library

Open
Misc
2
ObjectLink(object, displayText)
Not possible
You can’t pass entire rows into a Pack, so there is no equivalent.
Open
ParseCSV(csvString, delimiter)
Needs library

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

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

Open
Embed(url, width, height, force)
Not possible
Not possible using pure JavaScript, but can be done in the .
Open
Hyperlink(url, displayValue)
Not possible
Not possible using pure JavaScript, but can be done in the Pack SDK.
Open
HyperlinkCard(url)
Not possible
Not possible using pure JavaScript, but can be done in the Pack SDK.
Open
Image(url, width, height, name, style, outline)