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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

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

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay