JavaScript ES2024 ·
✓ verified by execution on 2026-08-11
Getting input from users is what makes your programs interactive. In the browser environment, one of the simplest ways to ask for input is using the prompt() method.
The prompt() method instructs the browser to display a dialog with an optional message prompting the user to input some text. It halts the execution of your program until the user either submits the text or cancels the dialog.
Asking for User Input
You can pass a message to prompt() to tell the user what you want them to type. If the user clicks “Cancel”, it returns null.
One thing to know before you run anything on this page: prompt() belongs to the browser window, and the sandbox these examples execute in has no window to open a dialog in. So each example below begins by defining its own stand-in prompt that returns a fixed answer, letting you see exactly what your code does with each possible reply without a dialog interrupting. Delete that first line when you copy this into a real page — there, the browser supplies the real prompt and a box appears.
javascript
const prompt = (msg, def) => null; // stand-in: pretends the user pressed Cancellet sign = prompt("What's your sign?");console.log(sign);
Output
null
Your output
You can even leave out the message entirely if there is nothing to show in the prompt window.
javascript
const prompt = (msg, def) => null; // stand-in: pretends the user pressed Cancellet value = prompt();console.log(value);
Output
null
Your output
Handling the Return Value
When the user enters text and clicks “OK”, prompt() returns a string containing the text entered by the user. If they cancel, it returns null. This is a great place to use an if/else statement.
Step through the execution below to see how the logic branches:
javascript · visualize
const prompt = (msg, def) => def; // stand-in: pretends the user accepted the defaultlet name = prompt("Enter name", "John");if (name === null) { console.log("Canceled");} else { console.log("Hello " + name);}
The Return Values of prompt()
You can also provide a default value that will be pre-filled in the text input field:
javascript
const prompt = (msg, def) => def; // stand-in: pretends the user accepted the defaultlet color = prompt("Favorite color?", "blue");console.log(color);
Output
blue
Your output
Common Misconceptions
One of the biggest traps for beginners is assuming that if a user types numbers into a prompt, JavaScript will treat it as a number.
Predict the outputjavascript
What does typeof age print?
const prompt = (msg, def) => def; // stand-in: pretends the user accepted the defaultlet age = prompt("Age?", "42");console.log(typeof age);
Output
string
You predicted
The return value is always a string, even if the user enters a number. If you need to do math with the input, you must explicitly convert it using a function like Number().
Another thing to watch out for is when the user submits the prompt without typing anything at all:
javascript
const prompt = (msg, def) => def; // stand-in: pretends the user accepted the defaultlet text = prompt("Enter text", "");console.log(text === "");
Output
true
Your output
Clicking OK with an empty input field returns an empty string "", which is different from clicking Cancel (null).
Edge Cases
Null Returns: Be sure to always handle the null case. Trying to treat null as a string (like calling a string method on it) will crash your program.
Empty Strings: A user can submit the prompt empty, resulting in an empty string "" instead of null.
Check yourself
If you type 42 into a prompt and click OK, what is the type of the returned value?
Reveal answer
A string containing "42" — prompt() always returns a string (or null). You must explicitly convert it using Number() or parseInt() if you want a number.
What does prompt() return if the user clicks the Cancel button?
Reveal answer
explicitly null — Clicking Cancel returns explicitly null, not an empty string. An empty string is returned when the user clicks OK without typing anything.
Can you style the prompt() dialog with CSS to match your website?
Reveal answer
No, it is a system-level modal controlled by the browser — The prompt() dialog is a system/browser-level modal. Its appearance is entirely determined by the browser and operating system, and cannot be styled with CSS.
Challenges
Challenge 1 +15 XP
Use prompt() to ask the user for their name with the message 'Name?'. If they enter a name, log 'Welcome, [name]!'. If they cancel (null), log 'Welcome, guest!'.
javascript
Test 1 (input: "Alice\n") — expects "Welcome, Alice!\n"
Test 2 (input: "null\n") — expects "Welcome, guest!\n"
Need a hint? (−25% XP)
Check if name === null
Show solution (0 XP)
import fs from 'node:fs';const prompt = (msg) => { const s = fs.readFileSync(0,'utf-8').trim(); return s==='null' ? null : s; };let name = prompt('Name?');if (name === null) { console.log('Welcome, guest!');} else { console.log('Welcome, ' + name + '!');}
Challenge 2 +20 XP
Ask for the user's age using prompt('Age?'). Convert the input to a number and log the age they will be next year (age + 1). If they cancel, log 'Canceled'.
javascript
Test 1 (input: "10\n") — expects "11\n"
Test 2 (input: "null\n") — expects "Canceled\n"
Need a hint? (−25% XP)
Use Number(ageStr) to convert.
Show solution (0 XP)
import fs from 'node:fs';const prompt = (msg) => { const s = fs.readFileSync(0,'utf-8').trim(); return s==='null' ? null : s; };let ageStr = prompt('Age?');if (ageStr === null) { console.log('Canceled');} else { let age = Number(ageStr); console.log(age + 1);}