← Back to Course

⌨️ Terminal Input and Output

Terminal programs can display information and ask the user for input without needing a browser interface.

Write Output

Example: Basic Output

console.log("Welcome to the app");
console.error("Something went wrong");

Read Input

Example: Ask a Question

import readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

async function run(): Promise<void> {
  const rl = readline.createInterface({ input, output });
  const answer = await rl.question("What is your name? ");
  console.log(`Hello, ${answer}`);
  rl.close();
}

run();

For a deeper version of this topic, see the dedicated resource page in the Extra Resources section.

← Back to Code Foundations