SKU // 129

JS Popup Alert

JavaScript has three built-in popup boxes: one to tell you something, one to ask a yes-or-no question, and one to ask for a line of text. They are the quickest way to talk to a person, and they are also blunt, ugly, and a little rude. This lesson covers all three, what each one returns, and what to use in real pages instead.

The three boxes are properties of window, so you call them as plain functions:

console.log(typeof alert, typeof confirm, typeof prompt);
function function function
Box What it does Returns
alert(message)shows a message with an OK buttonundefined
confirm(message)asks a question with OK and Canceltrue (OK) or false (Cancel)
prompt(message, default)asks for text, with OK and Cancelthe text typed (a string), or null if Cancel

// alert: Tell the Person Something

alert shows a message and waits for OK. It has nothing to return, so you use it only for the message itself. A \n inside the text starts a new line:

<button id="go">Show an alert</button>

<script>
  document.getElementById("go").addEventListener("click", function () {
    alert("Saved!\nYour work is safe.");
  });
</script>

// confirm: Ask Yes or No

confirm returns true if the person clicks OK and false if they click Cancel (or close the box). Use the answer in an if:

<button id="del">Delete the file</button>
<p id="result"></p>

<script>
  document.getElementById("del").addEventListener("click", function () {
    const ok = confirm("Delete this file for good?");
    document.getElementById("result").textContent = ok ? "Deleted" : "Cancelled";
  });
</script>

// prompt: Ask for Text

prompt returns what was typed, always as a string. The second argument is a starting value shown in the box. Three outcomes need handling:

The person... prompt returns
typed something, then OKthe text, like "Ava"
left it empty, then OKan empty string ""
clicked Cancelnull
<button id="ask">Ask my age</button>
<p id="answer"></p>

<script>
  document.getElementById("ask").addEventListener("click", function () {
    const text = prompt("How old are you?", "16");
    const out = document.getElementById("answer");

    if (text === null) {
      out.textContent = "You cancelled";
    } else if (text.trim() === "" || Number.isNaN(Number(text))) {
      out.textContent = "That is not a number";
    } else {
      out.textContent = "Next year you will be " + (Number(text) + 1);
    }
  });
</script>
Prompt text is always a string
prompt("Age?") returning "16" and then text + 1 gives "161". Convert with Number(text) first, and check for null and for text that is not a number.

// Why They Are Not Used Much Now

They are still handy for a quick test while you are learning, or for debugging. For a real page, show a message inside the page instead.

// A Better Way: the dialog Element

HTML has a built-in <dialog> element that you can style like any other part of the page. showModal() opens it with a dimmed background, and close() closes it. It does not freeze your code:

<button id="open">Open dialog</button>

<dialog id="box">
  <p>Delete this file for good?</p>
  <button id="yes">Delete</button>
  <button id="no">Cancel</button>
</dialog>

<p id="result"></p>

<script>
  const box = document.getElementById("box");
  const result = document.getElementById("result");

  document.getElementById("open").addEventListener("click", function () { box.showModal(); });
  document.getElementById("yes").addEventListener("click", function () { result.textContent = "Deleted"; box.close(); });
  document.getElementById("no").addEventListener("click", function () { result.textContent = "Cancelled"; box.close(); });
</script>
"Popup" can mean two things
This lesson is about dialog boxes. A separate meaning of "popup" is a new browser window opened with window.open (see the Window lesson). Browsers block those unless the person clicked something first.

// Quick Check

What does confirm("Sure?") return when the person clicks Cancel?

  1. null
  2. false
  3. undefined
  4. An empty string
Show answer
B. confirm returns true for OK and false for Cancel. prompt is the one that returns null on Cancel.

If the person types 5 into this box and clicks OK, what is total?

const text = prompt("Number?");
const total = text + 1;
Show answer
"51". A prompt always returns a string, so + joins instead of adding. Use Number(text) + 1 to get 6.

Give two reasons real websites avoid alert, confirm, and prompt.

Show answer
Any two of: they freeze the page and all code until dismissed, they cannot be styled, browsers may suppress them, and they interrupt people (including screen reader users).

// Try It

  1. Click Open Playground and click the button. Try OK, then Cancel, then leave the box empty
  2. Change the Playground so the greeting uses the name the person typed
  3. Add a confirm before the greeting: "Ready?" and say nothing if they cancel
  4. Replace the confirm in the Delete example with the dialog element
// Video Walkthroughs
Last updated: Oct 11, 2026, 7:05 PM ET