User Input in JavaScript
prompt() in the browser. Node has no prompt unless you install something.
BeginnerBasic ProgramsExample 3 of 10
user-input.js
1// Getting user input using prompt()2let name = prompt("Enter your name:");3let age = prompt("Enter your age:");45console.log("Hello, " + name + "! You are " + age + " years old.");67// Using template literals (modern approach)8console.log(`Hello, ${name}! You are ${age} years old.`);
Output
Enter your name: Alice Enter your age: 25 Hello, Alice! You are 25 years old. Hello, Alice! You are 25 years old.
What's going on
prompt() only exists in the browser. It always returns a string. Number(prompt(...)) or the next line is "10" + "20" = "1020".
In Node this file will crash. Use readline, or run it in the console.