JavaScript uses popup boxes to show quick messages or ask for direct input. These boxes stop the flow of the page until you take an action.
Table of Content
Understand the JavaScript Popup Boxes
JavaScript has three types of pop-up boxes. Each box has its own way to direct user interaction.
- The alert box shows a message and waits for you to press OK.
- The confirm box asks a question and gives OK or Cancel as choices.
- The prompt box asks for text input and returns what you type.
Alert Box in JavaScript
An alert box shows a single message on the screen. It forces the user to read it before returning to the page.
Here is the syntax:
alert("message");
Confirm Box in JavaScript
A confirmation box gives you two choices. You press OK or Cancel, and the code acts based on the choice.
The syntax looks like this:
confirm("message");
Prompt Box in JavaScript
A prompt box asks you to type a value. It returns what you type, so the script can use it.
Let’s see an example for each one in the following section.
Examples
Alert Box with Simple Message:
alert("Welcome to the site!");
This example shows a small message that says “Welcome to the site!” The box stops other actions until you press OK to close it.
Confirm Box with Conditional Response:
let answer = confirm("Do you want to log out?");
if (answer) {
alert("You logged out.");
} else {
alert("You stayed logged in.");
}
This code asks a yes or no type of question. If you press OK, the code shows a logout message. If you press Cancel, it shows that you stayed in.
Prompt Box with User Input:
let name = prompt("Enter your name:");
alert("Hello " + name + "!");
This code asks for a name and then shows it inside an alert. It shows how the prompt can pass values into later messages.
Prompt with Number and Calculation:
let num = prompt("Enter a number:");
let square = num * num;
alert("Square of the number is " + square);
This example asks for a number and then multiplies it by itself. The final alert shows the square of the value you entered.
Wrapping Up
You learned how JavaScript uses alert, confirm, and prompt boxes. And you also saw how each type works with real code examples.
Here is a quick recap:
- The alert box shows one message.
- The confirm box asks for a choice.
- The prompt box asks for input.
FAQs
What are JavaScript Popup Boxes and how do they work?
- alert() shows a simple message
- confirm() asks the user to choose
- prompt() collects user input
<script>
alert("Welcome to Popup Boxes");
</script>
How do you use alert, confirm, and prompt in JavaScript?
- alert() shows a message
- confirm() returns true or false
- prompt() returns user input
<script>
alert("Hello User");
var choice = confirm("Do you agree?");
var name = prompt("Enter your name:");
</script>
What is the difference between alert, confirm, and prompt boxes?
- alert() only displays a message
- confirm() lets user pick OK or Cancel
- prompt() asks the user for input
<script>
alert("Simple message");
var result = confirm("Continue?");
var input = prompt("Type something:");
</script>
Are JavaScript Popup Boxes still used today?
<script>
alert("Quick test popup");
</script>
Similar Reads
The forEach loop in JavaScript allows you to go through each item in an array. You do not have to…
Arrow function gives you short code and keeps this in the right scope in JavaScript. Understand the Arrow Function in…
The for...of loop appeared to solve the problem of complex loops over arrays and strings. It gives you a way…
Math.atan() function in JavaScript helps you to solve math problems in your code. It turns a number into an angle.…
Comments in JavaScript help you explain code and prevent mixing. They guide anyone who reads or edits your script. Understand…
JavaScript Ninja Code points to ways that help a person write code that runs fast and stays easy to read.…
The current essay is devoted to the basic principles and introduction of JavaScript. This language no longer needs to be…
JavaScript syntax is what you will use when writing and reading javascript code, it is the foundation of using this…
You may need to loop through properties when you work with objects in JavaScript. The for...in loop helps you do…
In this guide, you will learn how JavaScript functions work and why they matter. Each section shows what you need…