Alert, prompt, confirm in JavaScript

In this tutorial, I will discuss alert, prompt, and confirm in JavaScript. We will see how alert, prompt, and confirm work. These are built-in JavaScript methods. In the browser, they are built into the window object and allow us to display messages in a model way.

alert()

An alert box displays text in a dialog box with an “ok” button that pops up on the screen. While the alert box is displaying, the execution of the script remains paused and the user can’t proceed further until presses the “ok” or “ESC” key. Alert is used to inform or give messages to users through dialog box like ‘ are you 18+’, ‘right click is disabled’..etc…

alert("Are you 18+");

You can pass JavaScript statements also

alert(4*5);

If you pass any JavaScript statement like 4*5 as I passed, you will get the result in the dialog box which pops up on the screen.

Execute a function

function codespeedy(){
  return "Hey Codespeedy";
}
alert(codespeedy());

prompt()

Prompt box is used for taking value from the user as input by displaying a dialog box. It pops up with “ok” and “cancel” buttons and the user can’t proceed further until presses the  “ok” or “cancel” button. If the user puts a value and presses “ok” the box returns the input value or if the user presses “cancel” the box returns null.

let name = prompt("what is your name");

Check: Create prompt dialog box and take dynamic input from user in JavaScript

confirm()

Displaying a message the conform method displays a confirm box with “ok” and “cancel” buttons. We use confirm box when we want a user to confirm or verify or accept an action. To proceed further user will have to click either “ok” or “cancel” when a confirm box pops up. If the user presses “ok” the confirm method returns true otherwise false.

let dlt = confirm("Are you confirm about delete it")

Leave a Reply

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