One core characteristic of JavaScript is that it is built around non-blocking I/O. I will not expand on that term here, but it does mean JavaScript does not work like C where you can simply scanf and then printf.
There is a design reason behind this. JavaScript was originally created as a language meant to run inside the browser. In that environment, code is generally expected to keep moving forward rather than stop and wait for direct console input. Data is usually provided through constants, variables, or API calls.
If you still want an experience similar to blocking console input, Node.js provides a built-in way to do it:
const readline = require("readline");
const rl = readline.createInterface({ input: process.stdin, output: process.stdout,});
rl.question("Enter a number: ", (answer) => { const num = Number(answer); console.log(`The number you entered is: ${num}`); rl.close();});If you already know npm, you can also use a third-party package such as prompt-sync:
const prompt = require("prompt-sync")();
const name = prompt("Enter your name: ");const age = Number(prompt("Enter your age: "));
console.log(`Hello, ${name}. You are ${age} years old.`);That said, for front-end learners at that stage, I would recommend moving directly to browser APIs instead, such as reading input values from form fields to test functions with different inputs.
As for output, JavaScript output is very simple: use console.log.
console.log("Hello World"); // prints "Hello World"let x = 1;console.log(x); // prints 1