EthoVision XT 19 - JavaScript custom variables
Last updated: Jul 29, 2026
JavaScript Custom Variables
Aim
To expand EthoVision XT's analysis capabilities by introducing calculations, custom expressions, and search functions in JavaScript code embedded in EthoVision XT. There is a wide range of questions that can be answered with custom JavaScript variables. Here are a few examples:
- Calculate the average of coordinates x and y of a number of subjects.
- Calculate the number of subjects present in a zone.
- Find the body length of an animal by taking the distance between the nose-point and the tail-base point.
- Calculate the nearest neighbor distance in a school of fish.
- Recalculate the coordinates of a subject based on a reference point.
- Calculate the running average of a variable.
- Find the time when the animal reaches 80% of correct choices in a learning test.
- Create bins of velocity and assign each sample to one of them depending on the speed of the subject.
You can find JavaScript variables ready to use on the EthoVision XT 19 installation package, under Drivers and tools\JavaScript custom variables. You can also find the variables on GitHub at https://github.com/noldus/EthoVision-JavaScriptCustomAnalysis. Simply copy the code to the JavaScript window in an Analysis profile of EthoVision XT (see below). You can often customize the code by adjusting specific parameters.
Calculation
When you define JavaScript code in EthoVision XT, the software considers the data available at sample Sk and produces a value JSk (numeric, or boolean) based on the JavaScript code specified.
Sk represents a generic sample, which contains not only the x,y coordinates of the subjects but also their body area, elongation, and mobility at the current time. You can extract a lot of information and process it in the code.
The output values can be in one of three formats:
- A rational number, like the average of speed of three subjects. This is an example of a JavaScript continuous variable.
- A point event, like the event that occurs when a certain variable reaches the threshold of 80%. This is an example of a JavaScript event variable.
- A state event, like the time that a variable stays above a certain threshold. This is an example of a JavaScript state variable.
Remember that the JavaScript code outputs one or more values (depending on the code) per sample. Those values are used to calculate trial statistics and group statistics just like any other dependent variable; for example, the average and the standard deviation.
Procedure
- Open your Analysis profile.
- Choose one of the three options: JavaScript continuous, JavaScript event, or JavaScript state.
- Insert the code in the window that appears.
- Run analysis as usual.
Definition
A custom variable that processes raw data (for example, the subject coordinates, or the distance moved) and produces a numeric value for each sample of the track, based on an algorithm.
The JavaScript continuous variable outputs a rational number; other solutions are possible with JavaScript state (with a duration) and JavaScript event (with no duration). See Commands and Functions for JavaScript Variables for more information.
Use Cases
- Subject counter. Count the number of subjects currently present in a zone. For example: How many fish swim at the top of the tank? How many fruit flies enter each side of the T-maze?
- Subject counter. Calculate the ratio between the number of subjects in zone A to the number of subjects in zone B.
- Body length. Calculate the body length as the distance of the nose-point to the tail-base point.
- Tail beat detector. Calculate the ratio between the distance moved by the tail-base and the distance moved by the center-point. This helps quantify tail beats in fish.
To Specify a JavaScript Continuous Variable
- In the Analysis profile, under Custom Variables, click the button next to JavaScript continuous.
- In the JavaScript Continuous window, enter the code you require. Important: Always specify the following functions:
Start(),Stop(), andProcess(), even when they do not include code lines. - Click Check to see if there are syntax errors. Note that this check is not thorough and does not guarantee that the code provides meaningful data.
- Complete the procedure to add the variable. See Calculate Statistics: Procedure for more information.
Notes
- When you use functions like
GetCenter, the coordinates are given in millimeters regardless of what measurement unit is selected in the Experiment Settings. Therefore, all calculations based on coordinates are given in millimeters. If your calibration is done in cm, divide the results by ten. Similarly, values of subject area obtained with the functionGetAreaare in mm². - Select a Number of outputs other than
1if the JavaScript variable is made of two or more outputs. For example, suppose your code creates three state outputs forb1,b2,b3:
SetOutput(0, b1);
SetOutput(1, b2);
SetOutput(2, b3);
Then select3as the Number of outputs. The value of each output is updated at each sample. - JavaScript is not the same thing as Java. Although the two programming languages may show similarities in syntax, there are profound differences, for example in the rules that govern how variables and functions work.
- EthoVision XT uses the Internet Explorer 11-compatible JavaScript engine. This engine does not support the following features:
- Strict equality operators, for example
===or!==. - Class expression (classes are only available through function expressions).
- Arrow function expression.
- Fill for arrays.
- The
Math.hypotfunction.
- Strict equality operators, for example
See Also
- JavaScript Event
- JavaScript State
- Commands and Functions for JavaScript Variables
Source: EthoVision XT 19 Help, Noldus Information Technology