Reading input (stdin)
Read what you type in the IDE's Input tab: all at once with fs.readFileSync(0), or line by line.
Runs in Zerethon IDE · Node.js 22
Official JavaScript documentation ↗
A run cannot ask questions while it runs: type the input in the Input tab before pressing Run. The simplest way to read it is to read all of standard input (file descriptor 0) at once.
main.js
const fs = require('fs');
const lines = fs.readFileSync(0, 'utf8').split('\n');
const name = lines[0];
const [a, b] = lines[1].split(' ').map(Number);
console.log(`Hello, ${name}! ${a} + ${b} = ${a + b}`);
Input (stdin)
Ada 3 4
Output
Hello, Ada! 3 + 4 = 7
main.js
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin });
let total = 0;
rl.on('line', (line) => (total += Number(line)));
rl.on('close', () => console.log('total:', total));
Input (stdin)
5 10 15
Output
total: 30
readline — the close event fires when the input ends.
Watch out: prompt() does not exist in Node.js — it is a browser function.