Learn JavaScript
Lesson 1 - Introduction To JavaScript
Lesson 2 - Basic JavaScript Syntax
Lesson 3 - Control Flow
Lesson 4 - Functions
Lesson 5 - Basic Data Structures
Lesson 6 - Basic String Operations
Lesson 7 - Basic Array Operations
Lesson 8 - Exception Handling
Lesson 9 - Packages
readline
Packagereadline is a built-in Node.js package that provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time.
readline
package is included by default with Node.js, so no installation is necessary.This example shows how to use the readline
package to prompt the user for input and display a response.
// Import Package const readline = require('readline'); // Create Interface const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Taking User Input rl.question('What is your name? ', (answer) => { console.log(`Hello, ${answer}!`); rl.close(); });
If you want to ask the user for their name and age and then display it, you can create multiple nested rl.question
calls:
// Import Package const readline = require('readline'); // Create Interface const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Asking for User Name and Age rl.question('What is your name? ', (name) => { rl.question('What is your age? ', (age) => { console.log(`Hello, ${name}! Your age is ${age}.`); rl.close(); }); });
This extended example demonstrates how to handle multiple inputs by nesting rl.question
calls, allowing you to collect and display both the user's name and age.