JavaScript Course Lesson 15 of 16
The Codex / JavaScript / User Input & Output

User Input & Output

console.log, prompt and alert in the browser, readline in Node.js — and why JavaScript has two completely different answers.

What it is, in plain English: Output means showing information to the user or to yourself as a developer. In JavaScript, console.log() is the universal tool — it prints to the browser's developer console or to the terminal in Node.js. Input is where JavaScript splits in two: in the browser, prompt() asks the user a question in a dialog box; in Node.js, you use the readline module to read from the terminal. These two homes (browser and Node.js) have different tools — this page covers both clearly.

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: 35

More 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
  });
});

Modern Node.js (v17+) also has a promise-based readline that works well with async/await — covered in the async reference pages.

Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.