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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

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 *