> 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/concepts/custom-javascript/javascript-files.md).

# JavaScript files

JavaScript files help organize custom functions and variables. They're perfect for storing reusable helpers and utilities within components and actions.

You can create a JS file from the *Actions* panel - simply select this option after clicking on the *plus* sign.

<figure><img src="/files/0pg76ASBichVwSZLhOyx" alt=""><figcaption></figcaption></figure>

All top-level variables and functions defined in a JS file are accessible in Components and Actions.

## Scope

JavaScript files are scoped based on their creation context:

* **App-level** files - can be utilized across any component or action within the app
* **Page-level** files - restricted to their specific page

When multiple functions with the same name are defined in a scope, then the most recent definition will take precedence.

## Cross usage

Functions and variables defined in one JS file can also be used in others.&#x20;

{% hint style="success" %}
Since JavaScript files are added to a page as scripts, you can only reference definitions from files loaded earlier.
{% endhint %}

<figure><img src="/files/9Rry8jUIzwO3AmbhHj2e" alt=""><figcaption></figcaption></figure>

## Global and custom libraries

JavaScript files can also contain *custom* libraries in the custom code section as well as *default* libraries like `moment` and `lodash`.

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

## Debugging

You can troubleshoot any issues you have with a JS file by inserting a console log statement and running the function within an action or component, for example:

```javascript
function dateWithLabel(date) {
  console.log('debug!', date);
  return 'Date: ' + moment(date).format('MM/dd/yyyy');
}
```

The logs will appear in the **Logs** panel, prefixed with the file name where the code resides.

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

{% hint style="info" %}
Code that contains invalid JavaScript and cannot be parsed is not incorporated into the page.
{% endhint %}


---

# 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.uibakery.io/concepts/custom-javascript/javascript-files.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.
