docs/versioned_docs/version-2.50.0-LTS/data-sources/custom-js.md
The Run JavaScript Code feature in ToolJet allows custom JavaScript code to be executed to enhance application interactivity. This feature is useful for performing calculations, generating values, or interacting with queries and components.
<div style={{paddingTop:'24px'}}>Parameters allow for dynamic control over the JavaScript code execution without altering the core script. This provides flexibility by allowing the same code to execute with different inputs.
Each parameter requires:
Once added, the parameter can be referenced in the code using the syntax: parameters.<name>.
Let's create a new parameter named newAlert and set the value as object Displaying the Parameter Value in an Alert Box and use the alert js method to show the value on the pop-up.
Syntax:
alert(parameters.newAlert)
When the query is triggered the alert will show the parameters value.
Parameters can also be used to trigger other queries and pass custom values. Below is an example of how to call one query from another by providing custom parameters.
Begin by creating a new RunJS query named multiply.
In this query, add the following parameters:
Add the following JavaScript Code:
return parameters.num1 * parameters.num2;
{{queries.multiply.data}}.Now, let's create another RunJS query called callMultiply, where we will invoke the multiply query created earlier using custom parameter values. Here's the code snippet for callMultiply:
queries.multiply.run({num1: 20, num2: 7})
By executing this code within callMultiply, we trigger the multiply query with specific values for its parameters.
With this setup, the multiply query can be called from other queries, such as callMultiply, by providing custom parameter values. This allows you to reuse the multiply query with different inputs and display the results accordingly.
</div> <div style={{paddingTop:'24px'}}>This example demonstrates how to generate and display a random number using JavaScript.
const a = Math.floor(Math.random() * (10 - 1)) + 1;
return a;
{{queries.runjs1.data}}. It will display the output as Random number: result from JS codeThe following code generates a unique ID in the format "id" followed by a sequence of random hexadecimal characters.
var id = "id" + Math.random().toString(16).slice(2);
return id;
For example, it could be something like "id2f4a1b".
In this code, the resulting ID will have the format "timestamp + randomHex", where "timestamp" is the current time in base-32 and "randomHex" is a random hexadecimal value.
return String(Date.now().toString(32) + Math.random().toString(16)).replace(/\./g, '');
This ID will be longer than the one generated earlier, and it could look like "2g3h1d6a4h3".
:::tip Resources
ToolJet allows you to internally utilize these libraries:
| Name | Documentation |
|---|---|
| Moment | https://momentjs.com/docs/ |
| Lodash | https://lodash.com/docs/ |
| Axios | https://axios-http.com/docs/intro |
:::info Issues with writing custom JavaScript code? Ask in our Slack Community. :::
</div>