Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Javascript | Window prompt() Method

  • Difficulty Level : Easy
  • Last Updated : 26 Jul, 2019

The prompt() method is used to display a dialog with an optional message prompting the user to input some text. It is often used if the user wants to input a value before entering a page.
It returns a string containing the text entered by the user, or null.


prompt(message, default)
  • message is a string of text to display to the user.It can be omitted if there is nothing to show in the prompt window i.e. it is optional.
  • default is a string containing the default value displayed in the text input field. It is also optional.


<!DOCTYPE html>
        Window prompt() Method
<body style="text-align: center;">
    <h1 style="color:green;">
        Window prompt() Method
    <button onclick="geek()">
        Click me!
    <p id="g"></p>
        function geek() {
            var doc = prompt("Please enter some text",
            if (doc != null) {
                document.getElementById("g").innerHTML =
                    "Welcome to " + doc;

Before clicking the button:

After clicking the Click me! button:

After pressing the OK button in dialog box:

Supported Browsers: The browser supported by window prompt() method are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!