---
title: "__javaScript"
description: "JMeter __javaScript function reference: syntax, parameters, and practical examples for parameterizing test plans with dynamic values."
url: https://docs.jmeter.ai/functions/javascript/
lastUpdated: 2026-10-01
source: docs.jmeter.ai
---

# __javaScript

*Also documented in context in the [full Functions and Variables reference](/user-manual/functions/#__javaScript).*

The javaScript function executes a piece of JavaScript (not Java!) code and returns its value

The JMeter Javascript function calls a standalone JavaScript interpreter.
Javascript is used as a scripting language, so you can do calculations etc.

> **Note**
> javaScript is not the best scripting language for performances in JMeter. If your plan requires a high number of threads
> it is advised to use `__jexl3` or `__groovy` functions.

For Nashorn Engine, please see [Java Platform, Standard Edition Nashorn User’s Guide](https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/).

For Rhino engine, please see [Mozilla Rhino Overview](http://www.mozilla.org/rhino/overview.html)

The following variables are made available to the script:

- `log` - the [Logger](https://www.slf4j.org/api/org/slf4j/Logger.html) for the function
- `ctx` - [JMeterContext](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html) object
- `vars` - [JMeterVariables](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html) object
- `threadName` - String containing the current thread name
- `sampler` - current [Sampler](https://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html) object (if any)
- `sampleResult` - previous [SampleResult](https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html) object (if any)
- `props` - JMeterProperties (class [`java.util.Properties`](https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html)) object

Rhinoscript allows access to static methods via its Packages object.
See the [Scripting Java](https://wiki.openjdk.java.net/display/Nashorn/Rhino+Migration+Guide) documentation.
For example one can access the JMeterContextService static methods thus:
`Java.type("org.apache.jmeter.threads.JMeterContextService").getTotalThreads()`

> **Note**
> JMeter is not a browser, and does not interpret the JavaScript in downloaded pages.

| Name | Required | Description |
| --- | --- | --- |
| Expression | Yes | The JavaScript expression to be executed. For example:              - `new Date()` - return the current date and time - `Math.floor(Math.random()*(\${maxRandom}+1))`                     - a random number between `0` and the variable `maxRandom` - `\${minRandom}+Math.floor(Math.random()*(\${maxRandom}-\${minRandom}+1))`                     - a random number between the variables `minRandom` and `maxRandom` - `"\${VAR}"=="abcd"` |
| Variable Name | No | A reference name for reusing the value                 computed by this function. |

> **Note**
> Remember to include any necessary quotes for text strings and JMeter variables. Also, if
> the expression has commas, please make sure to escape them. For example in:
>
> ```bash
> \${__javaScript('\${sp}'.slice(7\,99999))}
> ```
>
> the comma after `7` is escaped.

Examples:

```bash
\${__javaScript(new Date())}
```

will return `Sat Jan 09 2016 16:22:15 GMT+0100 (CET)`

```bash
\${__javaScript(new Date(),MYDATE)}
```

will return `Sat Jan 09 2016 16:22:15 GMT+0100 (CET)` and store it under variable `MYDATE`

```bash
\${__javaScript(Math.floor(Math.random()*(\${maxRandom}+1)),MYRESULT)}
```

will use maxRandom variable, return a random value between 0 and maxRandom and store it in MYRESULT

```bash
\${__javaScript(\${minRandom}+Math.floor(Math.random()*(\${maxRandom}-\${minRandom}+1)),MYRESULT)}
```

will use `maxRandom` and `minRandom` variables, return a random value between `maxRandom` and `minRandom` and store it under variable `MYRESULT`

```bash
\${__javaScript("\${VAR}"=="abcd",MYRESULT)}
```

will compare the value of `VAR` variable with `abcd`, return `true` or `false` and store the result in MYRESULT

## Related

- [Functions and Variables Guide](/topics/functions-and-variables/)
- [Regex Extractor Builder](/tools/regex-tester/)
- [Full Functions Reference](/user-manual/functions/)
- [Component Reference](/user-manual/component-reference/)
