A program needs to show its results. JavaScript has several ways to do this, from a pop-up box to a change in the page itself.
Dialog Boxes
JavaScript has three built-in dialog boxes. The alert() function shows a message with an OK button. The confirm() function shows a question with OK and Cancel, and returns true or false. The prompt() function asks the user to type text and returns the text, or null if the user cancels. Dialogs stop the page until the user answers, so they should be used sparingly.
Definition: Dialog box. A small window made by alert, prompt or confirm that waits for the user to respond.
Table 3.9: The three dialog boxes
| Function | Shows | Returns |
|---|---|---|
| alert(message) | A message and OK | Nothing |
| confirm(question) | OK and Cancel | true or false |
| prompt(question) | A text box | The text, or null |
Console Output
The method console.log() prints values to the browser console, which is the developer's main tool. It accepts several values separated by commas. Related methods are console.error() for errors, console.warn() for warnings and console.table() for arrays and objects. The console is not seen by ordinary visitors.
Writing to the Page
The method document.write() writes text into the page while it is loading. If it is called after the page has finished loading, it erases the whole page and replaces it, so it is rarely used in real code. The better way to show output is to change an element with textContent, as the previous topic showed.
Choosing the Right Output
Use the console while developing, a dialog for a quick question, and a change to an element for output that every visitor should see.
Example 3.9: A confirmation flow
<p id="msg">Waiting...</p>
<script>
const name = prompt("Your name?") || "guest";
const ok = confirm("Show greeting for " + name + "?");
document.getElementById("msg").textContent =
ok ? "Hello, " + name : "Greeting cancelled";
console.log("name:", name, "ok:", ok);
</script>
The dialogs run before the message is changed. If the user types Asha and presses OK in both boxes, the paragraph reads Hello, Asha. If the user cancels the prompt, the program falls back to the name guest.
Debugging with the Console
The console is the first place to look when a script does not work. An error message there names the file and the line, and tells what went wrong, for example that a variable is not defined or that a value is null. Printing values with console.log("total is", total) shows what the program holds at each step. The sources panel of the browser tools can also pause a script on a chosen line, so that the values can be inspected one by one.
Common mistakes
- Using alert for every message, which interrupts the user again and again.
- Using document.write after the page has loaded, which wipes the page.
- Forgetting that a cancelled prompt returns null.
- Leaving console.log calls in a finished page.
Key takeaways
- alert shows a message, confirm asks a yes or no question and prompt asks for text.
- console.log is for developers, and a changed element is for visitors.
- A cancelled prompt returns null.
- document.write after loading erases the page.
Practice questions
- What does confirm return?
- Where is the output of console.log seen?
- Why is document.write rarely used?
Answers to the practice questions
- true if the user presses OK and false if the user presses Cancel.
- In the browser's developer console.
- Called after the page loads, it replaces the whole page.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III
