The Node REPL

The REPL is an interactive shell for trying JavaScript instantly.

Syntaxnode

Type node with no file name to open the REPL (Read-Eval-Print Loop). It is an interactive prompt where you can run JavaScript one line at a time and immediately see the result.

Why use it?

  • Quickly test a snippet or an idea.
  • Explore what a built-in function does.
  • Do quick calculations.

Press Ctrl + C twice, or type .exit, to leave the REPL.

Example

Example · bash
$ node
Welcome to Node.js v20.11.1.
> 2 + 3
5
> const name = 'Ada'
undefined
> `Hello, ${name}`
'Hello, Ada'
> .exit

When to use it

  • A developer quickly tests a new Array method like `Array.from({length:5}, (_,i)=>i*2)` in the REPL before writing it into the codebase.
  • A student explores how `require('path').join` behaves with different inputs without creating a temporary file.
  • A debugging session uses the REPL to inspect a live object's prototype chain and methods interactively.

More examples

Start and use the REPL

Launches the interactive REPL and evaluates expressions -- type `.exit` or Ctrl+C twice to quit.

Example · bash
$ node
> 2 + 2
4
> 'hello'.toUpperCase()
'HELLO'
> .exit

Test a built-in module in REPL

Requires a core module directly in the REPL to explore its API without writing a script file.

Example · js
// Inside the node REPL:
const os = require('os');
os.platform();    // 'linux'
os.cpus().length; // number of CPU cores

Multi-line input in REPL

The REPL accepts multi-line function definitions using the `...` continuation prompt.

Example · bash
$ node
> function greet(name) {
...   return `Hello, ${name}!`;
... }
undefined
> greet('World')
'Hello, World!'

Discussion

  • Be the first to comment on this lesson.