Execute JavaScript with the Code block
Learn how to use JavaScript code in a workflow.
JavaScript is the primary method for manipulating and transforming data in Retool. Use Code blocks to write custom JavaScript code that can transform data and perform complex logic. You can also use popular JavaScript libraries to further extend the functionality of workflows.
Add a JavaScript Code block
To use JavaScript in a workflow, add a Code block to the canvas and select JavaScript.
Write and execute JavaScript code
You can build complex logic or manipulate data using JavaScript methods like map(). For example, you could transform an array of customer records using map():
const data = query1.data;
return data.map((customer) => ({
fullName: customer.name,
emailAddress: customer.email,
}));
In general, Retool recommends you visually construct conditional statements with Branch blocks or filter query results using Filter blocks.
JavaScript Code blocks can also call functions—reusable blocks that operate outside the workflow control flow. Functions allow your workflow to perform actions only when required and can receive parameters to use.
Use JavaScript libraries
Retool includes support for a selection of popular JavaScript libraries which you can use in a workflow. You can browse and add libraries, configure their imports, and use them in your workflow.
Preloaded libraries
Retool automatically imports the following utility libraries for use across Retool. These libraries are available for use in JavaScript queries and within {{ + + }} embedded expressions.
| Name | Alias | Description |
|---|---|---|
| Lodash | _ | A modern JavaScript utility library delivering modularity, performance & extras. |
| Moment.js | moment | Parse, validate, manipulate, and display dates and times. |
| UUID | uuid | Generate and validate RFC-compliant UUIDs. |
| Numbro | numbro | Convert, format, and manipulate numbers. |
| PapaParse | Papa | Parse CSV data, and convert between CSV and JSON. |
| i18next 1 | i18n | Manage the translation of a web application. |
Footnotes
-
Available in web apps only. Not available in workflows or mobile. ↩
Built-in libraries
Retool also provides a series of built-in libraries, which are third-party libraries that you can import.
To use a built-in JavaScript library:
- Open the Libraries tab.
- Click +, and click Add JavaScript Library.
- The pop-up window shows the most commonly used libraries, or you can search for the library that you want to include.
- Toggle the checkbox to make it available, and click Add selected libraries when you are done.