> For the complete documentation index, see [llms.txt](https://docs.digia.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.digia.tech/logic-and-interaction/adding-logic/functions.md).

# Functions

### 🧩 Using JavaScript Functions (Advanced)

In addition to built-in expressions, you can write JavaScript functions and invoke them from within the Logic dialog using:

```javascript
js.eval('fnName', arg1, arg2, ...)
```

The JavaScript functions must be defined in the following format:

```javascript
function fnName(args) {
  const arg1 = args[0];
  const arg2 = args[1];
  // your logic
}
```

> ⚠️ **Note:** Arguments are automatically collected as an **array**, so you should extract individual arguments from the `args` array in your function body as shown above.

***

#### ⚠️ Important Considerations

* JavaScript functions **incur a small performance overhead** as the expression engine needs to cross into the JavaScript runtime.\
  👉 **Avoid using them in rapidly updating UIs such as scrolling lists or infinite lists.**\
  Instead, **pre-process** the data before passing it to a ListView.
* JavaScript functions that work in the **Dashboard preview** (browser) might **not work in the app** for advanced **date/time scenarios**.\
  This is because the app runtime uses **QuickJS**, which has **limited support for some Date APIs**.\
  Use basic Date parsing/formatting only, and prefer Digia’s built-in `DateTime` functions whenever possible.

***

#### ✅ Examples

Define functions in your script:

```javascript
function multiply(args) {
  const a = args[0];
  const b = args[1];
  return a * b;
}

function formatTimestamp(args) {
  const timestamp = args[0];  // e.g. 1691939250000
  const date = new Date(timestamp);
  return date.toISOString().split('T')[0];  // ✔️ basic date formatting
}
```

Invoke them in your expression:

```javascript
js.eval('multiply', 6, 7)
```

```javascript
js.eval('formatTimestamp', response.data.createdAt)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.digia.tech/logic-and-interaction/adding-logic/functions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
