Skip to content
Related Articles

Related Articles

Difference between alert box and prompt box in JavaScript

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Apr, 2022

1. Alert box : An alert box is one type of popup boxes in JavaScript which is often used to make sure that information have come through the user. So, the user will have to click “OK” to proceed when an alert box pops up on the window. 

Syntax :

window.alert("message");

Another syntax to use alert box is :

alert("message");

2. Prompt box : It is also one of the type of popup boxes in JavaScript which is often to take input a value before entering a page from the user. To proceed after entering an input value in the prompt, the user will have to click either “OK” or “Cancel”. 

Syntax :

window.prompt("some message");

Another syntax to use alert box is :

prompt("some message");

Below example illustrates the differences between alert box and prompt box : Example : 

HTML




<!DOCTYPE HTML>
<html>
 
<head>
    <title>
        Alert Box vs Prompt Box
    </title>
</head>
 
<body style="text-align:center;" id="body">
 
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
     
    <button onclick="alertBox()">
        Show Alert Box
    </button>
    <button onclick="promptBox()">
        Show Prompt Box
    </button>
     
    <script>
        function alertBox(){
            alert("GeeksforGeeks: This" +
                    " is an Alert Box.");
        }
 
        function promptBox(){
            prompt("Enter your Name:");
        }
    </script>
</body>
 
</html>                    

Output: 

Before clicking the button :

  

After clicking the Show Alert Box button :

  

After clicking the Show Prompt Box button : 

 

Supported Browsers: The list of browser supported by prompt() and alert() box are listed below :

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

Let us see the differences in a tabular form -:
 

 alert boxprompt box
1.An alert box is used if we want the information comes through to the user.A prompt box is used when we want the user to input a value before entering a page.
2.

Its syntax is -:

window.alert(“sometext”);

Its syntax is -:

window.prompt(“sometext”,”defaultText”);

3.It always return true we always need to click on “OK” to proceed further.We need to click “OK” or “Cancel” to proceed after entering an input value when a prompt box pops up on a webpage.
4.The alert box takes the focus away from the current window and forces the browser to read the message.If we click “OK” the box returns the input value.
5.We need to click “OK” to proceed when an alert box pops up.If we click “Cancel” the box returns a null value.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!