0% found this document useful (0 votes)
15K views

Alert Method in JavaScript

The alert() method displays an alert box with an OK button to convey important messages to users. It takes a string as a parameter and displays the string in the alert box. The user must click OK to continue. Other popup boxes include confirm() and prompt(). Basic usage involves calling alert() and passing a string. The syntax is alert(message). It converts non-string arguments to strings using toString(). Different data types like numbers, undefined, null, arrays, and objects are displayed differently in the alert box.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15K views

Alert Method in JavaScript

The alert() method displays an alert box with an OK button to convey important messages to users. It takes a string as a parameter and displays the string in the alert box. The user must click OK to continue. Other popup boxes include confirm() and prompt(). Basic usage involves calling alert() and passing a string. The syntax is alert(message). It converts non-string arguments to strings using toString(). Different data types like numbers, undefined, null, arrays, and objects are displayed differently in the alert box.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

How to Use the alert method in

JavaScript

The Window alert() method displays an alert box


containing text and an OK button.
In addition to alert(), there are two other types of popup boxes – confirm and prompt.
The alert box only allows the user to click the OK button. It is intended to be used to convey
important messages to the user that do not require a response, simply acknowledgement.

The users will not be able to access the program’s interface until they click the OK button,
confirming that they have read the message.

Basic examples
Below is a basic example of creating an alert using JavaScript:
alert('Please read our terms and conditions');

This line of code will display an alert window containing the words “Please read our terms
and conditions”, along with a button that reads “OK”. This syntax is also equivalent to the
following code:
window.alert('Please read our terms and conditions');

This code performs the same task as the first example, but explicitly uses the window object
to generate the alert.

Syntax
The syntax for the Window.alert() method is as follows:

alert(message)
message (optional): a String specifying the text that will appear in the popup box. This
message will appear above an “OK” button. If data types other than Strings are provided as
the message parameter, the alert() method will try to convert the data into a string by
applying the toString() method.

Examples of Alerts With Different Data Types


The following examples demonstrate using different data types as arguments for
the alert() method’s message parameter:
Number
alert(1);
// Expected text message: 1

undefined
alert(undefined);
// Expected text message: undefined

null
alert(null);
// Expected text message: null

Array
alert([22, 15, 67]);
// Expected text message: 22, 15, 67

Note: The square brackets are removed from the array during the string conversion process.
Object
const user = { name: 'John', age: 36 };
alert(user);
// Expected text message: [object Object]

Note: the default behavior of applying the toString() method to an object returns [object,


object] as the converted string. To avoid this behavior, use JSON.stringify() or a for…
in statement to convert the object to the desired string.
For example, the following is the text message returned for the above user object when
using JSON.stringify():
alert(JSON.stringify(user));
// Expected text message: {"name":"John","age":36}

Note: the object’s keys are now wrapped in double quotes.


Symbol
let sym = Symbol('foo')
alert(sym)
// Expected output:
// Uncaught TypeError: Cannot convert a Symbol value to a string

When alerting with a Symbol we receive a TypeError. To get around this, apply
the toString() method to the symbol as seen below:
alert(sym.toString())
// Expected text message: Symbol(foo)

Related Articles:
JavaScript – How to Use the toString method
JavaScript – How to Use JSON.stringify
JavaScript – How to Use the for…in Statement

Related Posts

How to Redirect to a Different URL with JavaScript


 4 min read
 October 1, 2020
There are many situations in which you might want to redirect a user to a new URL as they
use your application. For example, releasing a

How to Use the toString method in JavaScript


 6 min read
 November 3, 2020
JavaScript’s toString() method receives an argument, returning the object’s text
representation as a string. The string value returned depends on the type of the argument that
How to Use the String.prototype replace Method in
JavaScript
 6 min read
 December 6, 2020
The replace() method operates on a String, returning a new string with the specified 

You might also like