Favorites

DOM Window.prompt() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Window.prompt()
DOM Window MethodThis method is supported by all modern browsers.
Updated 28th November 2026

The prompt() method shows a dialog asking the user to type some text, and returns what they entered or null.

Description

The prompt() method displays a modal dialog with a message and a text box, returning the entered string, an empty string if they entered nothing, or null if they cancelled. It blocks the page and can't be styled or validated, so a form or <dialog> is better for real input.

The returned value is always a string (or null), so convert it with Number() if you asked for a number, and validate it, since users can type anything. Like alert() and confirm(), it pauses all scripts while open and is blocked in sandboxed iframes.

Because it can't be styled, labelled properly or validated as the user types, a form field, or a form inside a <dialog>, is a better choice for anything users see in production.

Syntax

prompt(message)
prompt(message, defaultValue)

Parameters

  • messageThe text to show.
  • defaultValueOptional. The initial text in the input box.

Return Value

The text the user entered, or null if they cancelled.

Example

<button id="ask" type="button">Ask my name</button>
document.getElementById('ask').addEventListener('click', () => {
  const name = prompt('What is your name?', 'Guest');
  console.log(name === null ? 'Cancelled' : 'Hello, ' + name);
});

Browser Support

The following information will show you the current browser support for the Window.prompt() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee