The Node.js REPL

Experiment with JavaScript interactively and use useful REPL commands.

Lesson content

๐ŸŽฏ Goal By the end of this lesson, you'll understand: What is the Node.js REPL? Why do we use it? How to start and exit the REPL Common REPL commands Real-world examples Interview questions What is the Node.js REPL? REPL stands for: R โ†’ Read E โ†’ Evaluate P โ†’ Print L โ†’ Loop It is an interactive environment where you can write and execute JavaScript code one line at a time. Simple Definition: The Node.js REPL is like a JavaScript playground where you can test code instantly. Real-Life Example ๐Ÿงฎ Imagine using a calculator. You type: 10 + 20 The calculator immediately shows: 30 The Node.js REPL works the same way. You Type โ”‚ โ–ผ Node.js Executes โ”‚ โ–ผ Shows Result What Does REPL Mean? 1. Read Node.js reads your input. 2 + 3 2. Evaluate It executes the code. 2 + 3 โ†“ 5 3. Print The result is displayed. 5 4. Loop It waits for the next command. > This cycle continues until you exit. How to Start the REPL Open your terminal and type: node You'll see: Welcome to Node.js > The > symbol means the REPL is ready. Your First REPL Program > 10 + 20 30 > "Hello" 'Hello' > true true Variables > let name = "Ajay" undefined Why undefined ? Because variable declarations don't return a value. Now use it: > name 'Ajay' Functions > function add ( a , b ) { ... return a + b ; ... } Call it: > add ( 10 , 20 ) 30 Objects > const user = { ... name : "Ajay" , ... age : 30 ... } > user Output { name: 'Ajay', age: 30 } Arrays > const numbers = [ 1 , 2 , 3 ] > numbers . map( n => n * 2 ) Output [2, 4, 6] Using Node.js Globals > process . version Example Output 'v22.0.0' > process . platform Example Output 'win32' Multi-line Code REPL supports multi-line code. > if ( true ) { ... console . log( "Hello" ); ... } Output Hello The ... prompt means Node.js is waiting for the rest of the code. Useful REPL Commands Clear the Screen .clear Exit the REPL .exit or press: Ctrl + C Press Ctrl + C twice to exit. Get Help .help Shows all available REPL commands. Load a JavaScript File Suppose you have: math.js Load it into the REPL: .load math.js Save the Current Session .save session.js This saves everything you've typed into a file. Real-Life Example ๐Ÿ‘จโ€๐Ÿ’ป Suppose you're learning JavaScript. Instead of creating a new file every time: Create File โ”‚ โ–ผ Write Code โ”‚ โ–ผ Run File You can simply use the REPL: Open REPL โ”‚ โ–ผ Write Code โ”‚ โ–ผ See Result Instantly This makes learning and debugging much faster. REPL vs JavaScript File REPL JavaScript File Interactive Saved in a file Executes one line at a time Executes the whole file Great for learning and testing Best for real applications Temporary Permanent When Should You Use REPL? โœ… Test JavaScript expressions. โœ… Practice JavaScript syntax. โœ… Debug small code snippets. โœ… Explore Node.js global objects. โŒ Don't use REPL to build complete applications. Interview Question What does REPL stand for? Answer: R โ†’ Read E โ†’ Evaluate P โ†’ Print L โ†’ Loop What is the output? > const x = 5 undefined > x * 2 10 Reason: Declaring a variable returns undefined , but evaluating x * 2 returns 10 . Best Practices โœ… Use the REPL to quickly test JavaScript code. โœ… Experiment with Node.js APIs before using them in your project. โœ… Use .help to discover available commands. โœ… Use .clear to reset the current REPL context. Key Points โœ… REPL stands for Read, Evaluate, Print, Loop . โœ… It lets you execute JavaScript interactively. โœ… Start it by typing node in the terminal. โœ… Use .help , .clear , .load , .save , and .exit for common tasks. โœ… The REPL is ideal for learning, testing, and debugging small pieces of code. ๐Ÿง  Easy Formula Open Terminal โ”‚ โ–ผ Type: node โ”‚ โ–ผ > Write JavaScript โ”‚ โ–ผ See Result Instantly โ”‚ โ–ผ Repeat (Loop) Remember: ๐Ÿงช REPL = JavaScript Playground โšก Perfect for quick experiments and learning ๐Ÿ“ Use .js files for real applications