Introduction
The JavaScript alert() function allows us to display any warning or messages in a browser window. It is mostly used to convey important information to users. Whenever we use the alert() method, an alert box is appeared in a browser window, and the browser loses its focus on the current window. The users are forced to react “OK” button if they want to continue browsing the current window.
![JavaScript Alert](https://files.codingninjas.in/article_images/custom-upload-1683268272.webp)
So, in this article, we are going to cover the JavaScript alert() method in detail.
What is the JavaScript Alert function?
JavaScript Alert or alert() is an in-built function in JavaScript that can be used to display a dialog box on a user’s screen to alert the user about a specific event. JavaScript Alert can be used to confirm some action from the user where a message and OK button is rendered on the dialog box.
Here are the two ways javascript alert can be used:
1. alert(): This is an in-built function in JavaScript that is used to alert the user. Here’s an example using ‘alert()’.
alert(“This is an alert() dialog box”);
2. window.alert(): This is also an in-built function as ‘alert()’ but ‘window.alert()’ is more explicit because the ‘alert()’ method is called in the window object. Here’s an example using the ‘window.alert()’ method.
window.alert(“This is the window.alert() dialog box”);
Both alert() and window.alert() can be used interchangeably in most cases. But If a developer is working with a variable that is a conflict with alert(), window.alert() can be used.
Syntax of JavaScript alert()
The javascript alert() function consists of two types of syntax which are given below.
//syntax 1
alert();
//syntax 2
alert(message);
Parameters of JavaScript alert()
There is one optional parameter or argument in JavaScript alert(), which is mentioned below.
message: ‘string’
the message is an optional string that can be sent as a parameter in the alert() function if we want to display some text in the alert box.
Return Value of JavaScript alert()
The JavaScript alert() function does not return any value.