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);
| Box | What it does | Returns |
|---|---|---|
alert(message) | shows a message with an OK button | undefined |
confirm(message) | asks a question with OK and Cancel | true (OK) or false (Cancel) |
prompt(message, default) | asks for text, with OK and Cancel | the text typed (a string), or null if Cancel |
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 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 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 OK | the text, like "Ava" |
| left it empty, then OK | an empty string "" |
| clicked Cancel | null |
<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("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.
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.
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>
window.open (see the Window lesson). Browsers block those unless the person clicked something first.
What does confirm("Sure?") return when the person clicks Cancel?
nullfalseundefinedconfirm 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;
"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.
confirm before the greeting: "Ready?" and say nothing if they cancelconfirm in the Delete example with the dialog element