When i want to take user input in my code by taking command prompt vs code or node js editor says it is not defined. But as far i know we can take user input in JS by "prompt" .
example :-
function leapYear(year) {
if ((year %2 == 0) && (year %100 != 0) || (year %400 == 0)) {
console.log(year + " LeapYear");
}
else{
console.log(year +" Not a LeapYear");
}
}
const year = prompt('Enter a year:');
leapYear(year);
Uncaught ReferenceError: prompt is not defined
Yes you can get user input from the command line (so using node), use readline:
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = "";
rl.question("input here:\n", function (string) {
input = string;
console.log("input that was entered: " + input);
rl.close();
});
It's a bit more hassle than in other languages.
More here: https://www.educative.io/edpresso/how-to-get-user-input-from-command-line-with-javascript
There's a lot more to readline, so here's the official documentation: