# Javascript Rule Builder

## Getting Started

By default, every rule starts out on the visual rule builder which means you must opt into using JavaScript.

You can opt-in to using JavaScript by navigating to your rule, clicking the "Code Editor" tab and then clicking "Edit Code" which will prompt you about opting into using JavaScript.

Once you convert your rule to using JavaScript, you can go back to the visual rule builder at anytime by clicking the "Rule Builder" tab.

**Note:** Opting back into the visual rule builder will restore the last state of the rule builder meaning any changes to the JavaScript code will be lost.

## Feathery Context

The `feathery` object exposes the Context API to your rule's logic. Using this object, you can perform actions such as setting a field value, navigating to a step, setting custom errors and more. [Full reference](/develop/context-api.md).

## Accessing Form Fields

If the field's ID is a valid JavaScript variable name, then you can access the field directly through a field global that is pre-defined for your convenience.

For example:

```javascript
// Interacting with a field using it's ID
myTextField.value = "some interesting text";

// Another example
MySelect.options = ["First Choice", "Second Choice"];
```

**Note**: Field globals are only available in logic rules running inside Feathery, not in SDK call-backs running outside of Feathery.

If the field ID is not a valid JavaScript variable name, then you can access the field using `feathery.fields` which is an object containing all keys.

For example:

```javascript
// Interacting with a field that have invalid JavaScript variable names
feathery.fields["1st_name"].value = "first name";

// Another example
feathery.fields["last name"].value = "last name";
```

Each method of accessing the field will result in a `Field` object being returned. You can read more about the available methods and properties on the Field object [here](/develop/context-api/field-object.md).

## Rule Runtime

Rule are run synchronously in that a rule must complete before feathery runs other rules or continues with form processing.  However, network requests made by rules may be either done from within the browser or on the [server](/platform/build-forms/advanced-logic/javascript-rule-builder/api-connections-in-code.md).  In either case, the network request is asynchronous.  If you want that network request to complete before continuing, you must await the request call.  See an example of awaiting a network request [here](/platform/build-forms/advanced-logic/javascript-rule-builder/api-connections-in-code.md#example).


---

# Agent Instructions: 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:

```
GET https://docs.feathery.io/platform/build-forms/advanced-logic/javascript-rule-builder.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
