User Input & Output
console.log, prompt and alert in the browser, readline in Node.js — and why JavaScript has two completely different answers.
rl.question() doesn't wait. It shows the prompt, registers your callback for later and returns immediately, so code written on the lines after it runs before the user has typed anything. A variable filled in by the callback is still undefined there.
const readline = require("readline");
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
let name;
rl.question("What is your name? ", (answer) => {
name = answer;
rl.close();
});
console.log(`Hello, ${name}!`); // runs immediately -- before any answer arrives
// $ echo Asha | node greet.js
// What is your name? Hello, undefined!Showing output and getting input in JavaScript
The first thing every programmer does is print something to the screen.
In Python it's print(). In JavaScript, it's console.log().
console.log — your most-used tool
// Print a string:
console.log("Hello, world!");
// Print a number:
console.log(42);
// Print multiple things at once — they appear with spaces between:
console.log("Name:", "Alice", "| Age:", 30); // Name: Alice | Age: 30
// Print the result of an expression:
console.log(5 + 3); // 8
console.log(10 > 5); // true
// Print objects and arrays — shows their structure:
console.log({ name: "Alice", age: 30 });
console.log([1, 2, 3, 4]);
// Labelling values while debugging:
let total = 0;
total += 10;
console.log("total after first add:", total); // total after first add: 10
total += 25;
console.log("total after second add:", total); // total after second add: 35More console methods
// console.error — red output, for errors:
console.error("File not found");
// console.warn — yellow output, for warnings:
console.warn("This API is deprecated");
// console.table — renders arrays of objects as a table (great for data):
const students = [
{ name: "Alice", grade: "A" },
{ name: "Bob", grade: "B" },
{ name: "Charlie", grade: "A" }
];
console.table(students); // renders a neat table in the console
// console.group — group related logs:
console.group("User details");
console.log("Name: Alice");
console.log("Age: 30");
console.groupEnd();
// console.time — measure how long something takes:
console.time("loop");
for (let i = 0; i < 1_000_000; i++) {}
console.timeEnd("loop"); // "loop: 3.5ms"The two homes — browser vs Node.js input
This is where JavaScript's "two homes" split matters most. Getting input from a user is completely different in a browser versus Node.js.
🌐 In the browser
Use prompt(), alert(), and confirm() — they show dialog boxes to the user.
Note: These block the page while waiting. Modern web apps use HTML form elements instead — but prompt/alert are great for quick experiments and learning.
🖥️ In Node.js
Use the readline module to read from the terminal. Node.js doesn't have prompt() or alert().
Note: The Try It editor above runs in the browser, so prompt/alert work there but readline won't.
Browser: prompt, alert, confirm
// alert — show a message box (user clicks OK)
alert("Welcome to my app!");
// prompt — ask a question, returns what the user typed (or null if cancelled)
const name = prompt("What is your name?");
console.log(`Hello, ${name}!`);
// prompt always returns a string — convert if you need a number:
const ageStr = prompt("How old are you?");
const age = Number(ageStr);
if (isNaN(age)) {
alert("That's not a valid age!");
} else {
alert(`You are ${age} years old.`);
}
// confirm — yes/no dialog, returns true or false
const confirmed = confirm("Are you sure you want to delete this?");
if (confirmed) {
console.log("Deleted!");
} else {
console.log("Cancelled.");
}Node.js: readline
// Save this as input.js and run with: node input.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin, // read from the keyboard
output: process.stdout // show prompts in the terminal
});
rl.question("What is your name? ", (name) => {
console.log(`Hello, ${name}!`);
rl.question("How old are you? ", (ageStr) => {
const age = Number(ageStr);
console.log(`Next year you'll be ${age + 1}.`);
rl.close(); // important: close when done or the program won't exit
});
});That's why the example above nests the second question inside the first callback: anything that needs the answer has to run inside the callback (or after an await with the promise-based version below). The fix is to move the code that uses the answer into the callback:
rl.question("What is your name? ", (answer) => {
console.log(`Hello, ${answer}!`); // runs once the answer exists
rl.close();
});
// $ echo Asha | node greet.js
// What is your name? Hello, Asha!Modern Node.js (v17+) also has a promise-based readline that works well with async/await — covered in the async reference pages.
Official Sources
- MDN Web Docs — console
- Console Standard (WHATWG)
- Node.js Docs — readline
- MDN Web Docs — Window.prompt()
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.