Q1. What is Node.js REPL?
REPL stands for Read-Eval-Print-Loop. It's an interactive shell that comes with Node.js where you can execute JavaScript code line by line. To start it, simply type node in your terminal without any file name. It's useful for testing small code snippets, debugging, and learning JavaScript/Node.js features.
Q2. How do you use the Node.js REPL?
Open terminal, type node and press Enter. You'll see a > prompt. You can now type JavaScript expressions: > 2 + 3 (returns 5), > console.log('Hi') (prints Hi). Use .help to see available commands, .exit to quit, or press Ctrl+C twice. Underscore (_) holds the last result.
Q3. What are special commands in Node.js REPL?
Special commands start with a dot: .break to exit multi-line expression, .clear to reset context, .editor to enter editor mode, .exit to quit REPL, .help to see commands, .load to load a file, and .save to save REPL session to a file. These make REPL more powerful for development.
Q4. How do you write multi-line code in REPL?
In REPL, you can write multi-line code by using .editor command, which opens an editor buffer. Type your code, then press Ctrl+D to execute. Alternatively, you can start a block with { and press Enter - REPL continues to next line until you close the block with } and press Enter.
Q5. What are practical uses of REPL?
REPL is great for: testing small code snippets, exploring API behavior, debugging by inspecting objects, learning Node.js modules, and quick calculations. It's like a playground for JavaScript. Developers often use it to test regular expressions, try out new library functions, or verify syntax.
