Skip to content
Related Articles

Related Articles

Improve Article

Difference between alert box and prompt box in JavaScript

  • Last Updated : 09 Dec, 2020

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :