# jsPsych "Hello world" experiment In the long tradition of **"Hello world!"** examples, this tutorial creates an experiment that outputs the phrase "Hello world!" to the browser. Though useless as an actual experiment, the process is helpful for learning the basics of using the jsPsych library. ## Step 1: Choose your own (setup) adventure With the release of version 7.0 of jsPsych there are now three different ways that you can add jsPsych to your project. Which approach you choose will depend on what your goals are. * **I want the simplest possible setup**. This approach involves using scripts that are hosted on a CDN. You do not need to download or install anything to start using jsPsych. The limitation is that you cannot customize the library. For most experiments, this approach will be sufficient. * **I want to be able to do some customization, but have a simple setup.**. This approach involves downloading a bundle of scripts that make up jsPsych. *If you used jsPsych prior to version 7.0, this will feel like the most familiar approach*. Having your own copy of the scripts means that you can make modifications to the library such as tweaking plugin behavior. * **I want to use modern JavaScript tooling, like `npm` and `import` statements.** Great! You can install jsPsych, plugins for jsPsych, and extensions for jsPsych from npm. This approach allows you to integrate jsPsych into your favorite JavaScript frameworks and get the benefits of !!! info If you would like to use modern web development tools (e.g. ES6 modules, Node/NPM, webpack, Babel), you may want to check out the [jsPsych Builder](https://github.com/bjoluc/jspsych-builder) CLI utility. jsPsych Builder allows you to automate the experiment setup, spin up a development server, and transpile and bundle scripts and styles. Using jsPsych Builder will automate some of the steps in this tutorial, so if you prefer that option, you may want to switch to the getting started instructions on the jsPsych Builder GitHub page. ## Step 1: Download the jsPsych library Start by downloading the jsPsych library. The most recent version can always be found on the [GitHub releases page](https://github.com/jspsych/jsPsych/releases). *Note: the image below shows version 4.2, but the process is the same for the most recent version.* ![releasespage](/img/githubreleases.jpg) !!! warning We strongly recommend downloading the latest release of the code rather than downloading the zip file of the code via the *Big Green Button* on the GitHub site. Downloading the code via the *Big Green Button* may give you a copy of the library that is in development and contains bugs. ## Step 2: Create a folder to store your experiment files Create a folder on your computer to put the experiment files in. Once you've created the folder, open the downloaded archive from step 1, and move the extracted folder (called `jspsych-6.3.0` if using v6.3.0 of jsPsych) into the experiment folder. ``` 📂 My Experiment -- 📂 jspsych-6.3.0 ``` If you open up the `jspsych-6.3.0` folder you should see this structure. ``` 📂 My Experiment -- 📂 jspsych-6.3.0 ---- 📂 css ---- 📂 examples ---- 📂 plugins ---- 📄 jspsych.js ``` ## Step 3: Create a new HTML file To edit jsPsych code you'll need a programming-friendly text editor. A great free option is [Visual Studio Code](https://code.visualstudio.com/) (Windows, OSX, Linux). Once you've got a text editor that you like, create a new file in the experiment folder called `experiment.html` ``` 📂 My Experiment -- 📂 jspsych-6.3.0 -- 📄 experiment.html ``` ## Step 4: Add the bare-minimum HTML code There's some basic code that (nearly) all HTML documents have in common. Here's a typical bare-bones HTML document. ```html My experiment ``` Add the above code to the experiment.html file and save it. If you then open the file in a web browser, you should see a blank page and the title of the page will be 'My experiment'. ## Step 5: Import the jsPsych library To use jsPsych, add a ` ``` You may also want to import the jsPsych stylesheet, which applies a basic set of visual styles to the experiment to make it visually pleasing. This requires adding a `` tag to the `` section of the document. ```html My experiment ``` ## Step 6: Use the jspsych-html-keyboard-response plugin to print a message For the demo, we want to show some text on the screen. This is exactly what the [jspsych-html-keyboard-response plugin](../plugins/jspsych-html-keyboard-response.md) is designed to do. To use the plugin, we need to load it with a ` ``` Once the plugin is loaded, we can create an experiment using the plugin. To declare a trial that uses the html-keyboard-response plugin, we create a JavaScript object with the property `type` equal to `'html-keyboard-response'`. Then we can specify the other parameters of the plugin in the same object. To add JavaScript code directly to the webpage we need to add a set of ` ``` Now that we have the trial defined we just need to tell jsPsych to run an experiment consisting of this trial. This requires using the `jsPsych.init` function and specifying the `timeline` parameter. ```html My experiment ``` Once you've saved the file, open it in a browser. You should see "Hello world!" printed on the screen, and if you press a key on the keyboard, the text should disappear (ending the trial).