Table of contents
1.
Introduction
2.
What is the JavaScript Alert function?
2.1.
Syntax of JavaScript alert()
2.2.
Parameters of JavaScript alert() 
2.3.
Return Value of JavaScript alert()
3.
How does JavaScript alert() work?
4.
Use Cases of JavaScript Alert Method
5.
Implementation of alert() in JavaScript
6.
 
7.
Frequently Asked Questions
7.1.
What is alert in JavaScript?
7.2.
How to show alerts in JavaScript?
7.3.
Is alert still used in JavaScript?
7.4.
What is the use of alert in JavaScript explain with example?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

JavaScript alert()

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

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.

How does JavaScript alert() work?

The alert dialog box is generally used for displaying messages that do not require any response from the user. They are used to inform the users about something important usually. Whenever the alert() method is called in a window browser, the alert message removes the focus from the current window.

Until the user does not close the alert box, the browser window will remain inaccessible. This is why alert messages should be used in control as they can hinder the browsing experience of the user.

Use Cases of JavaScript Alert Method

There are multiple use cases of JavaScript Alert following:

  1. Validating Input values: JavaScript Alert can be used to validate the input values inputted by the user. For example, alerts can be used to confirm the typed password.
     
  2. Error Messages: JavaScript Alert can be used to show the error message to the user. For example, If the password is incorrect, then an alert can be displayed on the screen to show what an error is with the help of an error message.
     
  3. Inputting values: JavaScript Alert can be used to request the input values from the user. For example, alerts can be used to take the age of the user in the dialog box.
     
  4. Displaying Warnings: JavaScript Alert can be used to display the warning to the user in the dialog box. For example, If there are website is redirecting the user to a different web page, the dialog box can be shown to warn the user.
     
  5. Displaying Confirmations or Feedbacks: JavaScript Alert can be used to display the dialog box with confirming messages or feedback. For example, If the user has submitted the registration form, A confirmation message can be displayed to the user with a dialog box.

Implementation of alert() in JavaScript

Code:

<!DOCTYPE html>
<html>
<head>
<title>Coding Ninjas's JavaScript Alert Tutorial</title>
</head>
<body>

<!-- HTML -->
<h1> Welcome, Ninja! </h1>
<p>Click this button below to display an alert.</p>
<button onclick="showAlert()">Show JavaScript Alert</button>

<!-- JavaScript -->
<script>
	function showAlert() {
	alert("Hello! This is an alert() dialog box");
	}
</script>
</body>
</html>

 

Webpage:

Webpage for demonstrating Alert Box

Dialog Box:

Default Alert Box - Example

 

Frequently Asked Questions

What is alert in JavaScript?

In Javascript, the alert() is a method which is used to display a virtual box. It is often used or giving a warning message to the users. It shows an alert box which has some specified message which is optional, and an OK button.

How to show alerts in JavaScript?

The alert() function is used to show alerts in JavaScript. This function will show text in a dialog bx that pops up on the screen. Prior to the working of this function, we must call the showAlert() function.

Is alert still used in JavaScript?

Yes, we can still use alert in JavaScript. Using alert is fine as long as it is clear why there is an alert. Also, alert is not useful to of JS objects we can only display strings. The problem with using alert in debugging scenarios is that it disrupts program flow.

What is the use of alert in JavaScript explain with example?

An alert() in JavaScript is used to give a warning message to the users. For example, is one input filed needs to enter some text but the user does not give any input, then as a part of validation, we can use an alert box to give a warning message.

Conclusion

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. In this article, we discussed what a JavaScript Alert is, its use cases, and how to customize the JavaScript Alert. We also did an implementation of the JavaScript alert to clear the concept.

Live masterclass