```
```You can use the REPL to try out bits of JavaScript code before including them into your programs. The REPL also includes some handy shortcuts to make that process easier.

## Step 3 — Mastering REPL Shortcuts

The REPL provides shortcuts to decrease coding time when possible. It keeps a history of all the entered commands and allows us to cycle through them and repeat a command if necessary.

For an example, enter the following string:

- "The answer to life the universe and everything is 32"

This results in:

```
'The answer to life the universe and everything is 32'
```

If we’d like to edit the string and change the “32” to “42”, at the prompt, use the `UP`

arrow key to return to the previous command:

```
> "The answer to life the universe and everything is 32"
```

Move the cursor to the left, delete `3`

, enter `4`

, and press `ENTER`

again:

```
'The answer to life the universe and everything is 42'
```

Continue to press the `UP`

arrow key, and you’ll go further back through your history until the first used command in the current REPL session. In contrast, pressing `DOWN`

will iterate towards the more recent commands in the history.

When you are done maneuvering through your command history, press `DOWN`

repeatedly until you have exhausted your recent command history and are once again seeing the prompt.

To quickly get the last evaluated value, use the underscore character. At the prompt, type `_`

and press `ENTER`

:

```
```The previously entered string will appear again:

```
'The answer to life the universe and everything is 42'
```

The REPL also has an autocompletion for functions, variables, and keywords. If you wanted to find the square root of a number using the `Math.sqrt`

function, enter the first few letters, like so:

```
```Then press the `TAB`

key and the REPL will autocomplete the function:

```
> Math.sqrt
```

When there are multiple possibilities for autocompletion, you’re prompted with all the available options. For an example, enter just:

```
```And press `TAB`

twice. You’re greeted with the possible autocompletions:

```
> Math.
Math.__defineGetter__ Math.__defineSetter__ Math.__lookupGetter__
Math.__lookupSetter__ Math.__proto__ Math.constructor
Math.hasOwnProperty Math.isPrototypeOf Math.propertyIsEnumerable
Math.toLocaleString Math.toString Math.valueOf
Math.E Math.LN10 Math.LN2
Math.LOG10E Math.LOG2E Math.PI
Math.SQRT1_2 Math.SQRT2 Math.abs
Math.acos Math.acosh Math.asin
Math.asinh Math.atan Math.atan2
Math.atanh Math.cbrt Math.ceil
Math.clz32 Math.cos Math.cosh
Math.exp Math.expm1 Math.floor
Math.fround Math.hypot Math.imul
Math.log Math.log10 Math.log1p
Math.log2 Math.max Math.min
Math.pow Math.random Math.round
Math.sign Math.sin Math.sinh
Math.sqrt Math.tan Math.tanh
Math.trunc
```

Depending on the screen size of your shell, the output may be displayed with a different number of rows and columns. This is a list of all the functions and properties that are available in the `Math`

module.

Press `CTRL+C`

to get to a new line in the prompt without executing what is in the current line.

Knowing the REPL shortcuts makes you more efficient when using it. Though, there’s another thing REPL provides for increased productivity—*The REPL commands*.

## Step 4 — Using REPL Commands

The REPL has specific keywords to help control its behavior. Each command begins with a dot `.`

.

### .help

To list all the available commands, use the `.help`

command:

```
```There aren’t many, but they’re useful for getting things done in the REPL:

```
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the repl
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl
```

If ever you forget a command, you can always refer to `.help`

to see what it does.

### .break/.clear

Using `.break`

or `.clear`

, it’s easy to exit a multi-line expression. For example, begin a `for loop`

as follows:

- > for (let i = 0; i < 100000000; i++) {

To exit from entering any more lines, instead of entering the next one, use the `.break`

or `.clear`

command to break out:

```
```You’ll see a new prompt:

```
>
```

The REPL will move on to a new line without executing any code, similar to pressing `CTRL+C`

.

### .save and .load

The `.save`

command stores all the code you ran since starting the REPL, into a file. The `.load`

command runs all the JavaScript code from a file inside the REPL.

Quit the session using the `.exit`

command or with the `CTRL+D`

shortcut. Now start a new REPL with `node`

. Now only the code you are about to write will be saved.

Create an array with fruits:

- > fruits = ['banana', 'apple', 'mango']

In the next line, the REPL will display:

```
[ 'banana', 'apple', 'mango' ]
```

Save this variable to a new file, `fruits.js`

:

```
```We’re greeted with the confirmation:

```
Session saved to: fruits.js
```

The file is saved in the same directory where you opened the Node.js REPL. For example, if you opened the Node.js REPL in your home directory, then your file will be saved in your home directory.

Exit the session and start a new REPL with `node`

. At the prompt, load the `fruits.js`

file by entering:

```
```This results in:

```
fruits = ['banana', 'apple', 'mango']
[ 'banana', 'apple', 'mango' ]
```

The `.load`

command reads each line of code and executes it, as expected of a JavaScript interpreter. You can now use the `fruits`

variable as if it was available in the current session all the time.

Type the following command and press `ENTER`

:

```
```The REPL will output:

```
'apple'
```

You can load any JavaScript file with the `.load`

command, not only items you saved. Let’s quickly demonstrate by opening your preferred code editor or `nano`

, a command line editor, and create a new file called `peanuts.js`

:

```
```Now that the file is open, type the following:

```
console.log('I love peanuts!');
```

Save and exit nano by pressing `CTRL+X`

.

In the same directory where you saved `peanuts.js`

, start the Node.js REPL with `node`

. Load `peanuts.js`

in your session:

```
```The `.load`

command will execute the single `console`

statement and display the following output:

```
console.log('I love peanuts!');
I love peanuts!
undefined
>
```

When your REPL usage goes longer than expected, or you believe you have an interesting code snippet worth sharing or explore in more depth, you can use the `.save`

and `.load`

commands to make both those goals possible.

## Conclusion

The REPL is an interactive environment that allows you to execute JavaScript code without first having to write it to a file.

You can use the REPL to try out JavaScript code from other tutorials: