User Input in JavaScript
Read user input in JavaScript with prompt() and print a greeting. Learn why prompt returns a string and why Node has no prompt, with explanation and output.
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.`);
Sample Output
Enter your name: Alice
This is the prompt box text plus what the user typed. It is not printed by console.log.
Hello, Alice! You are 25 years old.
Printed twice: once with + and once with a template literal.
In this one
- How prompt() collects input in the browser
- Why input always arrives as a string
- How to turn text into a number with Number()
- How template literals insert variables
How it works
- 1
prompt shows "Enter your name:" and waits.
- 2
The typed text is stored in name.
- 3
A second prompt stores the age as a string.
- 4
The first console.log builds the sentence with +.
- 5
The second console.log builds the same sentence with a template literal.
Edge cases
Running in Node
ReferenceError: prompt is not defined.
prompt is a browser feature. Run the code in the browser console, or use the readline module in Node.
User clicks Cancel
prompt returns null.
The greeting would print Hello, null!. Check for null and show a friendly message instead.
Doing math with the age
age + 1 gives "251" for an input of 25.
The value is a string. Use Number(age) + 1 to get 26.
What's going on
This program asks the user for a name and an age, then prints a greeting. It shows how a program can take data from a person instead of using fixed values.
prompt() opens a small box in the browser with a text field. Whatever the user types comes back as a string, even if they type a number. A string is text. So if a user types 25, your program gets "25", not 25.
That detail matters when you do math. "10" + "20" gives "1020" because + joins strings. Wrap the value with Number(prompt(...)) when you need a real number.
The greeting is printed twice. The first line joins pieces with +. The second line uses a template literal. A template literal is a string inside backticks where ${name} drops the variable value straight into the text. Both lines give the same result. Most modern code uses the template version because it is easier to read.
prompt() only exists in the browser. If you run this file with Node, it crashes with "prompt is not defined". In Node you would use the readline module or a package. For practice, paste the code into the browser console. If the user clicks Cancel, prompt returns null, and the greeting will say null.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What type of value does prompt() return when the user types 18?
Pick an answer to check it.
Question 2
What happens if you run prompt() in Node?
Pick an answer to check it.
Questions people ask
Why does my number act like text?
prompt always returns a string. Convert it with Number() or parseInt() before doing math.
Can I style the prompt box?
No. The browser draws it. For a styled form, use an HTML input element instead.
Is prompt used in real websites?
Rarely. It blocks the page and looks plain. It is fine for learning and quick tests.