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