> ## Documentation Index
> Fetch the complete documentation index at: https://docs.maia.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Variable expressions

Variable expressions let you calculate or reshape a value when a component runs, instead of using a fixed value. For example, you can round a number, remove a file extension, or choose between two values based on a condition.

To use an expression, write standard JavaScript inside the `${}` notation in a component property, and refer to [variables](https://docs.maia.ai/docs/guides/variables.md) by name. For example, if `file_name` is a text variable, `${file_name.toUpperCase()}` returns the file name in upper case. Maia evaluates the expression when the component runs and uses the result as the property value.

To save a computed value for later components, use the [Update Scalar](https://docs.maia.ai/docs/components/update-scalar.md) component to set a variable to the result of an expression.

## Where expressions work

* Expressions work in string, number, and drop-down properties that accept variables. They aren't accepted in drop-down properties that don't accept variables.
* A variable's **Default** value isn't evaluated. For example, a default of `${new Date().toISOString().slice(0, 10)}` stays as literal text. To set a computed value, use an Update Scalar component at the start of the pipeline.
* If an expression in an Update Scalar component fails, the variable can keep its previous value without raising an error. Use a [Print Variables](https://docs.maia.ai/docs/components/print-variables.md) component to check the result.

## Examples

In these examples, `x` and `y` are number variables, and `name`, `path`, and `list` are text variables.

### Arithmetic

| Task | Expression |
| - | - |
| Round to the nearest integer | `${Math.round(x)}` |
| Round down | `${Math.floor(x)}` |
| Round up | `${Math.ceil(x)}` |
| Larger of two values | `${Math.max(x, y)}` |
| Smaller of two values | `${Math.min(x, y)}` |
| Basic arithmetic | `${x + 1}`, `${x * 100}`, `${x % 2}` |
| Absolute value | `${Math.abs(x)}` |
| Round to two decimal places | `${Math.round(x * 100) / 100}` |

Arithmetic can return floating-point rounding errors. For example, `-12.7 % 2` returns `-0.6999999999999993`. To avoid this, round the result, such as with `${Math.round((x % 2) * 10) / 10}`.

### Strings

| Task | Expression |
| - | - |
| Last value in a path or file name | `${path.split('/').pop()}` |
| File name without its extension (removes only the last extension, so `a.b.csv` becomes `a.b`) | `${name.replace(/\.[^.]+$/, '')}` |
| Text before the first dot (`a.b.csv` becomes `a`) | `${name.split('.')[0]}` |
| File extension only | `${name.split('.').pop()}` |
| Convert to upper case | `${name.toUpperCase()}` |
| Convert to lower case | `${name.toLowerCase()}` |
| Trim whitespace | `${name.trim()}` |
| Replace the first occurrence of a character | `${name.replace('-', '_')}` |
| Replace all occurrences of a character | `${name.replaceAll('-', '_')}` |
| Join values | `${'prefix_' + name}` |
| Part of a string | `${name.substring(0, 4)}` |
| Length of a string | `${name.length}` |

### Dates

| Task | Expression |
| - | - |
| Current date and time as text | `${new Date().toISOString()}` |
| Current date as text, in `YYYY-MM-DD` format | `${new Date().toISOString().slice(0, 10)}` |
| Year | `${new Date().getFullYear()}` |
| Month (0–11, where 0 is January) | `${new Date().getMonth()}` |
| Day of the month | `${new Date().getDate()}` |
| Day of the week (0–6, where 0 is Sunday) | `${new Date().getDay()}` |

`toISOString()` returns the date and time in Coordinated Universal Time (UTC). The `getFullYear()`, `getMonth()`, `getDate()`, and `getDay()` methods use the local time zone of the Maia runner, so near midnight they can return a different day from `toISOString()`.

### Yes/no checks

A yes/no check returns the text `true` or `false`, not a Boolean value. Store the result in a Text variable, and compare it against the text value in later components.

| Task | Expression |
| - | - |
| Contains a value | `${name.includes('test')}` |
| Starts with a value | `${name.startsWith('test')}` |
| Ends with a value | `${name.endsWith('.csv')}` |
| Is empty | `${name.length === 0}` |

### Simple choices

| Task | Expression |
| - | - |
| Choose between two values | `${x > 100 ? 'big' : 'small'}` |
| Fall back to a default value | `${name \|\| 'default'}` |

### Lists

Use these expressions with a text variable that contains comma-separated values.

| Task | Expression |
| - | - |
| Count the items | `${list.split(',').length}` |
| First item | `${list.split(',')[0]}` |
| Last item | `${list.split(',').pop()}` |


## Related topics

- [Project and pipeline variables](https://docs.maia.ai/docs/guides/variables.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.