Node.js: Code Outside the Browser / Node.js Fundamentals

REPL (Read-Eval-Print Loop)

In Node.js, REPL stands for Read-Eval-Print Loop. It is a simple, interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user.

Think of it like the Console in your web browser's DevTools, but for the backend.

👀 The Four Stages

The name describes exactly what the environment does in a continuous loop:

  1. Read: It reads the JavaScript code you type into the terminal.

  2. Eval: It evaluates (executes) that code.

  3. Print: It prints the result of the execution back to you.

  4. Loop: It waits for your next command and starts over.

🧪 Why is REPL useful?

  • ✅ For testing small bits of code quickly without writing a full file

  • ✅ To explore Node.js features interactively

  • ✅ Great for debugging

  • ✅ Beginner-friendly sandbox to experiment with JS concepts


🚀 How to Open REPL

  1. Open Command Prompt or Terminal

  2. Type: node

    This will enter you into the Node.js REPL environment.

  1. Now you can type JavaScript code directly!

🔁 Example of REPL in Action:

🔁 Example: Variables

🔁 Example: Arrays & Objects

🔁 Example: Built-in Modules

🔁 Example: Multi-line Input

REPL detects when a block isn't finished and lets you keep typing:

🛠️ Useful Shortcuts in REPL

Command

Purpose

.exit

Exit the REPL

Ctrl + C

Quit or cancel current input

Ctrl + D

Exit the REPL

.help

Show REPL commands

Ctrl + K (Windows) / Cmd + K (Mac)

Clears REPL screen