javaScript alert, prompt, confirm box Example

javaScript alert, prompt, confirm box Example

If you work with javascript – you must know the three important useful methods of javascript. There are alert(), prompt(), confirm().

In this JavaScript Popup boxes tutorial, you will learn about javaScript popup and its uses. Javascript popups are of three types: Alert Popup, Confirm Box Popup, Prompt Box Popup. When using pop-up box methods you do not need to write “window” prefix.

Learn JavaScript alert, prompt, confirm Popup Box

  • Alert Box JavaScript
  • Confirm Box JavaScript
  • Prompt Box JavaScript

Alert Box JavaScript

When you need information to access the user, an alert box is used. It disrupts the user’s activity. This is the reason why you need to use it carefully. When the alert box appears, you have to press the OK button to start your activity again.

Syntax Of Alert Box

window.alert("Alert Text!");
Example
alert("How are you?");

Confirm Box JavaScript

When you need to accept something from the user that we use A confirmation pop-up box. When the confirmation pop-up box is open and you will see the two buttons name close and ok. if you want to agree than click ok button and not agree to close.

Syntax Of Confirm Box

window.confirm("Confirmation Information");

Need to know that – If the “OK” button is clicked, the confirmation box returns “true” . if the “cancel” button is pressed return “false”

Example
var cnfirm = confirm("Confirm something!");
if (cnfirm == true) { 
   x = "OK was pressed";  
}  
else {
   x = "Cancel was pressed";  
}

Prompt Box JavaScript

When you need to enter some information before the user goes ahead Then we do use the prompt box. If the user wants to close the prompts box then he will have to press OK or Cancel button.

Syntax Of Prompt Box
window.prompt("Information Text","Default Text");
Example
function firstFunction() {
    var txt_alert;
    var person = prompt("Please enter your full name:", "Harry Potter");
    if (person  == null || person  == "") {
        txt_alert = "User cancelled the prompt.";
    } else {
        txt_alert = "Hello " + person + "! How are you today?"
    }
    document.getElementById("demo_test").innerHTML = txt_alert;
}

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

One reply to javaScript alert, prompt, confirm box Example

  1. It is so Awesome, I wish you to upload more about JavaScript for a biginner like me to understand more.
    Thanks so much.

Leave a Reply

Your email address will not be published. Required fields are marked *