JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Popup Boxes

In JavaScript you can create dialog boxes or popups to interact with the user.

JavaScript has three different types of popup boxes: Alert box, Confirm box, and Prompt box.


Alert Box

An alert box is the most simple popup box. It enables you to display a short message to the user.

It also includes "OK" button, and the user has to click this "OK" button to continue.

Syntax:
window.alert("msg")

The window.alert() method can be written without the window prefix.

Run code

Confirm Box

A confirm box is often used if you want the user to verify or accept something.

A confirm box looks similar to an alert box but it includes a "Cancel" button along with the "OK" button.

If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

Syntax:
window.confirm("msg")

The window.confirm() method can be written without the window prefix.

Run code

Prompt Box

A prompt box is often used if you want the user to input a value before entering a page.

A prompt box includes a text input field, an "OK" and a "Cancel" button.

If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.

Syntax:
window.prompt("msg", "defaultText")

The window.prompt() method can be written without the window prefix.

Run code

Note: The value returned by the prompt() method is always a string. This means if the user enters 15 in the input field, the string "15" is returned instead of the number 15.

Therefore, if you want to use the returned value as a number you must convert it, see How To Convert Data Types in JavaScript.


Display line breaks inside the dialog boxes

To display line breaks inside the dialog boxes, use newline character or line feed (\n); a backslash followed by the character n.

Run code

More Examples

This example demonstrates different types of dialog boxes supported by JavaScript:

Click the buttons below to display different dialog boxes:

Run code