-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
JavaScript provides simple ways to interact with users using popup dialogs. These are small windows that appear on the page to show messages, ask questions, or get input.
There are three main popup types:
Alert – show a message
Confirm – ask the user to choose OK or Cancel
Prompt – ask the user to enter input
These popups are part of the Browser Object Model (BOM) and are available via the window
object.
The simplest popup is the alert. It shows a message and an OK button.
<button onclick="showAlert()">Show Alert</button>
<script>
function showAlert() {
// Display a simple message
alert("Hello! Welcome to CodePractice.in");
}
</script>
Explanation:
The alert()
function shows a modal box with your message.
Users cannot interact with the page until they click OK.
Displaying important notifications
Warning messages
Simple greetings
The confirm popup asks the user a question with OK and Cancel buttons.
It returns a boolean value: true
if OK is clicked, false
if Cancel.
<button onclick="showConfirm()">Delete File</button>
<script>
function showConfirm() {
let result = confirm("Are you sure you want to delete this file?");
if(result) {
alert("File deleted successfully!");
} else {
alert("File deletion canceled!");
}
}
</script>
Explanation:
confirm()
returns true
or false
.
You can use this value to control what happens next.
Confirming actions like deleting files
Warning before leaving a page
Asking yes/no questions
The prompt popup asks the user to enter some input. It returns the text entered by the user, or null
if Cancel is clicked.
<button onclick="showPrompt()">Enter Name</button>
<script>
function showPrompt() {
let name = prompt("Please enter your name:", "Your Name");
if(name) {
alert("Hello, " + name + "! Welcome!");
} else {
alert("No name entered.");
}
}
</script>
Explanation:
The first argument of prompt()
is the message.
The second argument is optional and provides a default value.
Use the returned value to personalize content or process input.
Asking for a user’s name or email
Quick input for simple forms
Debugging or testing
You can use alert, confirm, and prompt together for a small interactive experience.
<button onclick="interactiveDemo()">Try Demo</button>
<script>
function interactiveDemo() {
alert("Welcome to the interactive demo!");
let ready = confirm("Do you want to continue?");
if(!ready) {
alert("Goodbye!");
return;
}
let color = prompt("What is your favorite color?", "Blue");
if(color) {
alert("Nice! " + color + " is a great color!");
} else {
alert("You didn’t enter a color.");
}
}
</script>
Explanation:
First, an alert welcomes the user.
Then a confirm asks if they want to continue.
Finally, a prompt asks for their favorite color.
Alerts display the final messages.
Use popups sparingly – too many popups annoy users.
Avoid critical functionality with alert/confirm – they block interaction.
Use for quick feedback or small tasks – not complex forms.
Combine with HTML elements for better user experience.
Always check input from prompt() – it may be empty or canceled.
You can create reusable popup functions for your website.
// Show alert
function showMessage(msg) {
alert(msg);
}
// Show confirmation and return result
function askUser(question) {
return confirm(question);
}
// Get input from user
function getInput(promptMsg, defaultValue="") {
return prompt(promptMsg, defaultValue);
}
// Example usage
if(askUser("Do you want to proceed?")) {
let name = getInput("Enter your name:");
if(name) showMessage("Hello, " + name + "!");
}
Explanation:
Functions make popups easier to reuse across your website.
They keep code clean and organized.
Blocking behavior – user must interact with the popup before doing anything else.
Browser design – popups look different in each browser.
Cannot style them – native popups cannot change color, size, or font.
User annoyance – avoid excessive use.
Modern websites often use custom HTML/CSS popups for a better experience, but
alert
,confirm
, andprompt
are still useful for quick testing, tutorials, and small tasks.
JavaScript Popup Alerts are simple, built-in tools for interacting with users:
alert() – Show a message with an OK button.
confirm() – Ask a question and get a boolean response (OK/Cancel).
prompt() – Ask for user input and get text.
Using these popups properly can help guide users, get quick input, or provide feedback. They are part of the window object and are available in all modern browsers.
How can you display a simple welcome message to users using alert()
?
Write a script that asks the user for confirmation before deleting a file using confirm()
.
How can you get the user’s name using prompt()
and display a greeting with alert()
?
Create a flow where an alert welcomes the user, a confirm asks if they want to continue, and a prompt collects their favorite color.
How can you check if the user clicked Cancel in a prompt()
and show a different alert message?
Write a function that takes a message as an argument and displays it using alert()
.
How can you use confirm()
to perform different actions based on whether the user clicked OK or Cancel?
Create a small interactive demo where the user enters their age using prompt()
and receives a message based on their input.
How can you reuse a function to display multiple alert messages on the same page?
Write a program that asks the user for their email using prompt()
and validates if something was entered before showing a thank-you alert.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts