# 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)
```
