The Node REPL
The REPL is an interactive shell for trying JavaScript instantly.
Syntax
nodeType 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
$ node
Welcome to Node.js v20.11.1.
> 2 + 3
5
> const name = 'Ada'
undefined
> `Hello, ${name}`
'Hello, Ada'
> .exitWhen 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.
$ node
> 2 + 2
4
> 'hello'.toUpperCase()
'HELLO'
> .exitTest a built-in module in REPL
Requires a core module directly in the REPL to explore its API without writing a script file.
// Inside the node REPL:
const os = require('os');
os.platform(); // 'linux'
os.cpus().length; // number of CPU coresMulti-line input in REPL
The REPL accepts multi-line function definitions using the `...` continuation prompt.
$ node
> function greet(name) {
... return `Hello, ${name}!`;
... }
undefined
> greet('World')
'Hello, World!'
Discussion