Skip to content

The module contains functions for interacting with the data generated by jsPsych plugins.


Parameter Type Description
properties object Object of key: value pairs to add to the data.

Return value

Returns nothing.


This method appends a set of properties to every trial in the data object, including trials that have already occurred and trials that have yet to occur. You can use this to record things like the subject ID or condition assignment.


Assigning a subject ID and condition code{subject: 1, condition: 'control'});


Parameter Type Description
format string Specifies whether to display the data in 'csv' or 'json' format.

Return value

Returns nothing.


Outputs all of the data collected in the experiment to the screen in either JSON or CSV format. This is a useful method for quick debugging when developing an experiment.


Using the on_finish callback function to show data at the end of the experiment

    experiment_structure: exp,
    on_finish: function() {'csv');



Return value

Returns the data collection of all data generated by the experiment.


This function is the standard starting point for accessing the data generated by the experiment. It returns a DataCollection object, which has several methods that can be used to further filter, aggregate, and view the data. These methods are described under the DataCollection section on this page.


// select all trials
var all_data =;

// get csv representation of data and log to console


Parameter Type Description
node_id string The id of the TimelineNodes to get data from.

Return value

Returns a DataCollection of all of the data generated in a specified TimelineNode.


Get all the data generated by a specified Timeline.


var current_node_id = jsPsych.currentTimelineNodeID();

var data_from_current_node =;



Return value

Returns a DataCollection object with all of the interaction events.


jsPsych automatically records a few different kinds of user interaction events. blur events occur when the user clicks on another window or tab during the experiment, indicating that they are no longer interacting with the experiment. focus events occur when the user clicks on the experiment window after having clicked somewhere else first (i.e., generated a blur event). fullscreenenter and fullscreenexit events are triggered by the browser entering and exiting fullscreen mode. However, fullscreenenter events only occur when the script switches the browser to fullscreen mode, e.g., with the jspsych-fullscreen plugin. Manually entering fullscreen mode does not trigger this event. fullscreenexit events occur whether the user manually exits fullscreen mode or the script exits fullscreen mode. This method returns the DataCollection containing all interaction events. This is useful for tracking whether the participant completed the task without diverting attention to other windows. Events are in the form:

    type: 'focus' or 'blur' or 'fullscreenenter' or 'fullscreenexit',
    trial: 10, // the trial number when the event happened
    time: 13042 // total time elapsed since the start of the experiment


var interaction_data =;
// log data to console in json format

Return value

Returns a DataCollection.


Gets all of the data generated in the same timeline as the last trial.


var lasttimelinedata =;

Return value

Returns a DataCollection


Gets the data collection containing all data generated by the last trial.


var lasttrialdata =;


Parameter Type Description
var_name string Which variable to get the value of.

Return value

Returns the value of a variable passed in through the query string.


For extracting a particular variable passed in through a URL query string.


// if the URL of the page is: experiment.html?subject=1234&condition=test

console.log('subject')) // logs "1234"
console.log('condition')) // logs "test"

Return value

Returns an object (associative array) of the variables in the URL query string.


For extracting variables passed in through a URL query string.


// if the URL of the page is: experiment.html?subject=1234&condition=test

var urlvar =;
console.log(urlvar.subject) // logs "1234"
console.log(urlvar.condition) // logs "test"


Parameter Type Description
data_object object Object of key: value pairs to store in jsPsych's data storage as a trial.

Return value

Returns nothing.


This method is used by jsPsych.finishTrial for writing data. You should probably not use it to add data. Instead use


// don't use this! data should only be written once per trial. use jsPsych.finishTrial to save data.

var trial_data = {
    correct: true,
    rt: 487


All data is stored in the DataCollection object. Using methods like and return DataCollections containing the experiment data. This is a list of all of the methods that are available to call on a DataCollection object.


Adds a set of properties to all items in the DataCollection. Similar to, except that it can be applied to a subset of the whole DataCollection by filtering down to a smaller DataCollection first.{subject_id: 123, condition: 'control'});


Adds a set of properties to the last trial in the DataCollection.{success: true});


Counts the number of trials in the DataCollection.


Generates a CSV string representing all of the data in the DataCollection.



Returns a subset of the DataCollection based on the filter. The filter is an object, and trials are only kept in the returned DataCollection if they contain the key: value pair(s) in the filter object. For example, the code below selects all of the trials with a correct response.

var correct_trials ={correct: true});

The object can have multiple key: value pairs, and the trials must match all of them in order to be included in the returned collection.

// keep only correct trials from the practice phase
var correct_practice_trials ={correct:true, phase: 'practice'});

The filter can also be an array of objects. In this case each object in the array acts as an OR filter. As long as the trial has all the key: value pairs of one of the objects in the array, it will appear in the returned collection.

// select trials from block 1 and block 5.
var trials =[{block: 1}, {block:5}]);

The filter method returns a DataCollection object, so methods can be chained onto a single statement.

// count the number of correct trials in block 1
var block_1_correct ={block:1, correct:true}).count();


This method is similar to the .filter() method, except that it accepts a function as the filter. The function is passed a single argument, containing the data for a trial. If the function returns true the trial is included in the returned DataCollection.

// count the number of trials with a response time greater than 2000ms.
var too_long ={
    return trial.rt > 2000;

.first() / .last()

Returns a DataCollection containing the first/last n trials.

var first_trial =;
var last_trial_with_correct_response ={correct: true}).last(1);
var last_10_trials =;


Returns a DataCollection with all instances of a particular key removed from the dataset.

// log a csv file that does not contain the internal_node_id values for each trial


Appends one DataCollection onto another and returns the combined collection.

// get a DataCollection with all trials that are either correct or
// have a response time greater than 200ms.
var dc1 ={correct: true});
var dc2 ={ return trial.rt > 200});
var data = dc1.join(dc2);


Generates a JSON string representing all of the data in the DataCollection.



Saves a CSV or JSON file on the computer running the experiment. If conducting an online experiment, this will download the file onto the subject's computer, and is therefore not a recommended data storage solution for online data collection.

Warning: This function may not behave correctly in older browsers. Upgrading to the latest version of any major web browser should solve the problem.

// first argument is the format, second is the filename.
// the format can be either 'csv' or 'json'.'csv','mydata.csv');


Add a new entry to the DataCollection. This method is mostly used internally, and you shouldn't need to call it under normal circumstances.

var data = {correct: true, rt: 500};


Creates a copy of the DataCollection so that any modification of the values in the DataCollection will not affect the original.

// this line edits the rt property of the first trial[0].rt = 100;

// readOnly creates a copy that can be modified without affecting the original[0].rt
// outputs 100[0].rt = 200[0].rt
// still outputs 100


Returns a DataColumn object (see documentation below) of a single property from a DataCollection object.

var rt_data ='rt');


Generates an array of all the unique key names in the set of trials contained in the DataCollection. This is especially useful when setting up a relational database (e.g., MySQL) where the column names need to be specified in advance.



Returns the raw data array associated with the DataCollection. This array is modifiable, so changes to the array and values of objects in the array will change the DataCollection.

var raw_data =;

// was response in first trial correct?
} else {


DataColumn objects represent all the values of a single property in a DataCollection. They are generated by using the .select() method on a DataCollection. Once a DataColumn is generated, the following methods can be used.


Checks if all values in the DataColumn return true when passed to a function. The function takes a single argument, which represents one value from the DataColumn.

// check if all the response times in the practice phase were under 1000ms{phase: 'practice'}).select('correct').all(function(x) { return x < 1000; });


Counts the number of values in the DataColumn.

// count how many response times there are'rt').count();


Counts the number of occurrences of each unique value in the DataColumn. Returns this value as an object, where each key is a unique value and the value of each key is the number of occurrences of that key.

// get frequencies of correct and incorrect responses'correct').frequencies();

.max() / .min()

Returns the maximum or minimum value in a DataColumn.'rt').max();'rt').min();


Returns the average of all the values in a DataColumn.'rt').mean();


Returns the median of all the values in a DataColumn.'rt').median();


Returns the standard deviation of the values in a DataColumn.'rt').sd();


Filters the DataColumn to include only values that return true when passed through the specified function.

// below results will be less than 200.'rt').subset(function(x){ return x < 200; }).max();


Returns the sum of the values in a DataColumn.'rt').sum();


The raw array of values in the DataColumn.

// note that this is not a function call.'rt').values;


Returns the variance of the values in a DataColumn.'rt').variance();