> For the complete documentation index, see [llms.txt](https://docs.gpmautomate.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gpmautomate.com/automate-en/action-guides/javascript/execute-javascript-code.md).

# Execute JS code

Executing JS code is a very powerful action that allows you to deeply intervene in the browser by directly running JavaScript (JS) code snippets on the current webpage. This action helps you handle complex logic algorithms, perform mathematical calculations, extract advanced data, or carry out interactive operations that conventional No-code blocks do not yet support.

#### Configuration parameters:

* Path to js file or code: You can write/paste the JavaScript code directly into this box, or enter the absolute path to a `.js` file stored on your computer.
* Output variable: The variable name of GPM Automate to hold the returned value after the JS code runs.

#### ⚠️ Core notes when writing JS code in GPM Automate:

1. The `return` statement is mandatory: For the GPM Automate system to receive the processing result from the code and store it in the output variable, you must add the `return` statement at the end of the JS code. Without `return`, the output variable will receive an empty value (`undefined`). If your JS code does not need to return any value, then the `return` statement is not necessary.
2. This action **must** be placed within the Main logic block, similar to running JS code in the browser's Dev tools. You can test your JS code in Dev tools before using it in Automate.
3. Rules for embedding Automate variables into JS: You can call previously stored variables of GPM Automate within the JS code block using the syntax `$variable_name`.
   * For number type variables: You can write directly (e.g., `const index = $index;`).
   * For string type variables: You must wrap the variable in single quotes `'...'`, double quotes `"..."`, or preferably backticks `` `...` `` to avoid breaking the code format when the string contains whitespace or special characters, for example: ``const str = `$postContent`;``.

#### Practical example: Extracting a character based on the specified position (Index)

Suppose in the GPM Automate scenario, you already have 2 variables:

* Variable `$str` holding the string: `Hello, world!`
* Variable `$index` holding the number: `7`

You want to use JavaScript code to extract the character at the 7th position in the above string (the desired result is the letter `w`) and store it in the `$charResult` variable of Automate. You will configure the Execute JS code action as follows:

* Output variable: `charResult`
* JS code content:

```
const str = `$str`;
const index = $index;
const char = index >= 0 && index < str.length ? str[index] : "Invalid index";

return char;
```

Operational logic: The system will load the string `Hello, world!` and the number `7` into the code, run the validity check algorithm for the position, and extract the character. The `return char;` statement at the end will push the letter `w` out and load it directly into the `$charResult` variable for you to use in subsequent actions.

<figure><img src="/files/sOggYywn8VNOw5UYXGiE" alt=""><figcaption></figcaption></figure>


---

# 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.gpmautomate.com/automate-en/action-guides/javascript/execute-javascript-code.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.
