DEV Community

Cover image for JavaScript alert, prompt, and confirm
Bello Osagie
Bello Osagie

Posted on • Edited on

4 2

JavaScript alert, prompt, and confirm

alert

The alert function accepts a single argument. It displays the result on a modal dialog window.

The word "modal" means a user can't interact with the rest of the page.

The modal dialog window is a small box displayed on a web page.

Syntax:

alert(arg);
Enter fullscreen mode Exit fullscreen mode
alert('Hello World!');
Enter fullscreen mode Exit fullscreen mode

prompt

The prompt function accepts two arguments.

Syntax:

prompt(arg1[, arg2]);
Enter fullscreen mode Exit fullscreen mode
  • arg1 is required.
  • arg2 is optional.

On Internet Explorer, arg2 is required.

// For other browsers
const birthYear = prompt("What year were you born?");
alert(birthYear);

// For Internet Explorer
const yourName = prompt("What is your name?", "Michelle");
alert(yourName); 
Enter fullscreen mode Exit fullscreen mode

confirm

The confirm function only accepts an argument.

Syntax:

confirm(arg);
Enter fullscreen mode Exit fullscreen mode

If OK is clicked, the result is true and false otherwise.

const isDoctor = confirm("Are you a Doctor?");
alert(isDoctor); // true if OK button is clicked
Enter fullscreen mode Exit fullscreen mode

The above example is the same as below:

const isDoctor = prompt("Are you a Doctor?");
// const isDoctor = prompt("Are you a Doctor?", 'y/n'); IE

if (isDoctor === 'y') {
  alert("true");
} else if (isDoctor === 'n') {
  alert("false");
} else {
  alert("enter either y/n")
}  
Enter fullscreen mode Exit fullscreen mode


Buy me a Coffee


TechStack Media | Bluehost

  • Get a website with a free domain name for 1st year and a free SSL certificate.
  • 1-click WordPress install and 24/7 support.
  • Starting at $3.95/month.
  • 30-Day Money-Back Guarantee.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay