Code has been added to clipboard!

Main Tips on Creating JavaScript Alerts and Other Types of Popup Boxes

Reading time 3 min
Published Aug 8, 2017
Updated Oct 15, 2019

JavaScript alerts provide users with relevant information or with data that needs to be confirmed. The addition of JavaScript alerts makes websites more dynamic and informative.

This tutorial covers the three kinds of popups JavaScript has to offer: alert, confirm, and prompt. You will learn the usage of these different popups and the ways they improve websites.

JavaScript Alert: Main Tips

  • There are three types of JavaScript popup windows: Alert, Confirm and Prompt.
  • To write multiple lines in a JavaScript popup window, you will need to use "\n" string.
  • You do not need to use the window prefix when writing pop-up box methods.

Alert Box

A JavaScript alert box is used when you need some information to reach the user. When the alert box shows up, the user will need to press the OK button to resume activity. It interrupts the user's activity, so you need to be careful when using it.

You can write the following code to display a JavaScript alert box:

window.alert("Alert Text!");

The window prefix is not mandatory. You can write the code without it:

alert("Alert Text!");

The example below creates a pop-up alert that contains two lines of text:

Example
alert("Hello\nHow are you?");

Confirm Box

A confirm pop-up box is used when you need the user to accept something. When the confirm pop-up box opens, the user has to click one of the two buttons (OK or CANCEL) to close the box. OK returns true, while CANCEL returns false.

You can write the following code to display a confirm box:

window.confirm("Confirmation Information");

The window prefix is not mandatory here as well. You can write the code without it:

confirm("Confirmation Information");

The confirm box returns true if OK button is clicked, and returns false if CANCEL is pressed:

Example
var r = confirm("Confirm something!");
if (r == true) { 
   x = "OK was pressed";  
}  
else {
   x = "Cancel was pressed";  
}

Prompt Box

The prompt box is used when you need the user to insert some information before proceeding. To turn the prompt box off, the user has to click OK or CANCEL buttons.

The confirm box returns the input data if the OK button is clicked. It returns null if CANCEL button is clicked.

You can write the following code to display a prompt box:

window.prompt("Information Text","Default Text");

Just as before, the window prefix is not mandatory. You can write the code without it:

prompt("Information Text","Default Text");

See the example below to get a clearer idea, and click Try it Live! to see how it works:

Example
function firstFunction() {
    var txt;
    var person = prompt("Please enter your full name:", "Harry Potter");
    if (person  == null || person  == "") {
        txt = "User cancelled the prompt.";
    } else {
        txt = "Hello " + person + "! How are you today?"
    }
    document.getElementById("test").innerHTML = txt;
}

JavaScript Popup Boxes: Summary

  • If you want to display an alert of some kind to a user, use alert().
  • If you want to display a confirmation box with option buttons, use confirm().
  • If you want to use a pop up with an input and option buttons, which can display inputed values, use prompt().
What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen