JavaScript Window prompt() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.prompt() shows a dialog with a text field and waits for OK or Cancel (MDN). OK returns a string; Cancel returns null.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.prompt. Start with this line.

JavaScript
prompt() / prompt(msg) / prompt(msg, def)
CallWhat it does
prompt() / prompt(msg) / prompt(msg, def)Primary signature.
window.prompt(...)Call with the arguments your case needs.

Built into the browser. Call window.prompt(...) (or the global prompt(...)) — no import required.

Library declarations
JavaScript
prompt() / prompt(msg) / prompt(msg, def)

Parameters

ParameterDescription
messageOptional. Text shown above the input
defaultValueOptional. Pre-filled input text

Return Value

Return ValueDescription
*string or null

JavaScript Examples

Example 1 — Name with Default

Pre-fill the field, then greet on OK.

JavaScript
const userName = prompt("Please enter your name:", "Guest");

if (userName === null) {
  console.log("Canceled");
} else {
  console.log("Hello, " + userName + "!");
}
Try It Yourself

How It Works

  1. Pass two args. Message + default text.
  2. User edits or keeps it. Then clicks OK or Cancel.
  3. Guard cancel. Compare with null first.
  4. Use the string. Build a greeting from the result.

Example 2 — Cancel vs Empty

Three outcomes: cancel, blank OK, or typed text.

JavaScript
const userInput = prompt("Enter something:");

if (userInput === null) {
  console.log("Canceled");
} else if (userInput === "") {
  console.log("OK but empty");
} else {
  console.log("Entered:", userInput);
}
Try It Yourself

How It Works

  1. Call prompt. No default this time.
  2. null first. Cancel is not the same as blank.
  3. Empty string. User pressed OK with no text.
  4. Else branch. Real input to use.

Example 3 — Greet with alert

Combine prompt input with an alert message.

JavaScript
const name = prompt("What is your name?");

if (name !== null && name.trim() !== "") {
  alert("Nice to meet you, " + name + "!");
} else {
  alert("No name provided.");
}
Try It Yourself

How It Works

  1. Ask first. Store the prompt result.
  2. Trim blanks. Treat whitespace-only as missing.
  3. Show feedback. Use alert for a simple reply.
  4. Both block. Second dialog waits for the first to finish.

Example 4 — Validate a Number

Re-prompt until a positive number is entered, or stop on Cancel.

JavaScript
let age;
do {
  age = prompt("Enter your age (positive number):");
  if (age === null) break;
} while (age.trim() === "" || isNaN(Number(age)) || Number(age) <= 0);

if (age !== null) {
  console.log("Valid age:", age);
} else {
  console.log("Canceled");
}
Try It Yourself

How It Works

  1. Loop. Keep asking until input is valid or canceled.
  2. Break on null. Cancel exits cleanly.
  3. Cast + check. Use Number and reject blanks / NaN / ≤ 0.
  4. Report. Show the accepted age or a cancel message.

Example 5 — Favorite Color

Ask with a default and write the answer into the page.

JavaScript
const color = prompt("What is your favorite color?", "Blue");

if (color === null) {
  console.log("No color chosen.");
} else {
  console.log("Favorite color:", color);
}
Try It Yourself

How It Works

  1. Default helps. Users can accept or edit.
  2. Handle cancel. null means no choice.
  3. Show the value. Log or update the DOM.
  4. Production tip. Prefer a real color input or form.

Browser Support

Window.prompt() is marked Baseline Widely available on MDN (since July 2015). Browser Window API — not a Node.js built-in.

✓ Baseline · Widely available

prompt()

Ask for text with a modal dialog across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Fine for learning and quick demos. Prefer forms or

for production UI.

Frequently Asked Questions

It shows a modal dialog with an optional message, a text field, and OK/Cancel. The script waits until the user submits or cancels.
OK returns the entered text as a string (possibly empty). Cancel (or dismiss) returns null.
null means cancel. "" means OK with a blank field. Always check for null before using the value.
Yes. Pass a second argument: prompt("Your name?", "Guest").
No. MDN marks Window.prompt() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Rarely. It blocks the page and cannot be styled. Prefer forms or an HTML <dialog> for real apps; use prompt for learning and quick demos.

Next: resizeBy()

Change a window’s size by a relative pixel amount.

resizeBy() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful