> For the complete documentation index, see [llms.txt](https://docs.uibakery.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.uibakery.io/reference/working-with-actions/condition-action.md).

# Condition

The Condition step determines the next step in the action by evaluating a JavaScript expression. Return `true` to execute the <mark style="color:green;">if</mark> step, or `false` to execute the <mark style="color:red;">else</mark> step.

```javascript
return {{data.length}}  > 0;
```

## **Variables**

### **Built-in variables**

The following built-in variables are available for the Condition step:

```javascript
// result of the previous step
return {{data}};

// error response of the previous step
return {{error}};

// incoming action params, passed in by components,
// the Execution/Loop action steps or when calling the action from the code
return {{params}};

// the response of the request, if the Code step follows an HTTP API step
return {{res}};
```

While `{{data}}` and `{{error}}` are specific to a particular step, `{{params}}` is available in all steps.

### **Using variables**

```javascript
// use `{{data.<key>}}` to access a specific key
return {{data.name}};

// to access the first element of the array
return {{data[0]}};

// to access the `name` key of the first element
return {{data[0].name}};
```

### **Optional chaining**

If, at some point, the variable's value is `null` or `undefined`, an optional chaining operator `?.` can be used to access a specific key.

For example:

```javascript
// if `data` is `null` or `undefined`, `name` will not produce an error
return {{data?.name}};
// if `data` is `null` or `undefined`, `name` will not produce an error
return {{data?.[0]?.name}};
```

## Use case examples&#x20;

Execute the `true` step if the `data` array has some items:

```javascript
return {{data.length}}  > 0;
```

Execute the `true` step if the component's value is set:

```javascript
return !!{{ui.input.value}};
```

Check user role:

```javascript
return {{user.role}} === 'admin';
```

Check multiple user roles:

```javascript
return ['admin', 'manager'].includes({{user.role}});
```

Check that at least one user role is present:

```javascript
return ['admin', 'manager'].some(role => {{user.roles}}.includes(role));
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.uibakery.io/reference/working-with-actions/condition-action.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
