JavaScript Popup Boxes Interview Questions and Answers
Compare alert, confirm, and prompt dialogs and understand when their blocking behavior is appropriate.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- What are the different popup boxes available in core JavaScript?
- What is the purpose of `alert()`?
- Explain the functionality of a confirm box in JavaScript. How does it differ from an alert box, and when might you use it in a web application?
- What values can `confirm()` return?
- What does `prompt()` return when cancelled?
- What does it mean that browser dialogs are modal?
- What are the limitations of using an alert box in terms of user interaction and customization?
Theoretical JavaScript Interview Questions
Alert boxes are the blunt instrument of user communication. They block everything - your script pauses, and the user can only click OK. Here's what makes them painful:
The confirm() box is like alert()'s older sibling - it shows a message but gives the user two buttons: "OK" and "Cancel." It returns true or false based on their choice. You'll see it used for "Are you sure you want to delete this?" type prompts. Unlike alert(), it actually captures a decision from the user Read more . In production apps, custom modals are preferred because confirm() blocks execution and you can't customize the button text or styling.
The prompt() box is the only built-in dialog that collects text input from the user. It shows a message, a text field, and OK/Cancel buttons. Click OK, it returns the typed string. Click Cancel, it returns null. You can also pass a default value as the second argument Read more . Quick tip: always check for null before using the return value, since users can cancel. Like alert() and confirm(), it blocks execution, so use HTML form inputs for real applications.
There are three: alert() shows a message with an OK button (fire and forget), confirm() shows OK/Cancel and returns a boolean, and prompt() shows a text field and returns the input string (or null if cancelled). These are blocking calls - they freeze the entire page while open. Fine for quick debugging, but never use them in production UI; build a proper modal instead.
`alert()` requests that the browser display a message dialog to the user. It is modal and blocks interaction with the page until dismissed, so it is usually unsuitable for polished application feedback.
`confirm()` returns `true` when the user accepts and `false` when they cancel or dismiss the dialog. Browsers may suppress dialogs in some contexts.
`prompt()` returns the entered text as a string, or `null` if the user cancels or dismisses it. An empty submitted value is an empty string, distinct from `null`.
A modal dialog blocks normal interaction with the page until it is dismissed. It can interrupt workflows and is controlled by the browser, so accessible in-page UI is often a better choice.
Prompt input is text and may also be `null` on cancellation, so validate cancellation separately and parse the text before using it as a number or other structured value. User input should not be assumed valid.
No. Browsers can suppress dialogs in situations such as repeated prompts, background pages, or sandboxed frames, and some environments do not implement them. Essential application behavior should not depend on them.
Traditional alert, confirm, and prompt dialogs are synchronous from the calling script's perspective and pause its progress until dismissal. This makes them disruptive and can complicate event-driven user experiences.
Compare the result with `null` to detect cancellation; an empty submitted response is `""`. Do not use a truthiness check if those cases need different handling.
Native dialogs have browser-controlled presentation, limited styling and workflow integration, and may interrupt assistive-technology or keyboard flows. A carefully implemented accessible in-page dialog can provide clearer context and focus management.
Browsers limit disruptive dialogs to reduce abuse, and may suppress them when calls are not associated with a user interaction or when the page is inactive. Code should treat a dialog as optional UI rather than a guaranteed control flow mechanism.
Use a visible in-page status message or notification that does not unexpectedly block the user, and expose its updates accessibly, such as through an appropriate live region. Keep critical decisions in a purpose-built dialog with focus handling when necessary.