JavaScript

Javascript Alert

Javascript Alert

Javascript is the most known language of the web. Javascript is widely used in front-end development as well as in the back-end. Javascript provides a lot of built-in functions to help in development. In this article, we are going to learn one of the javascript's built-in alert() method, which is used to show pop-ups over the screen to either display a message or show a warning. The alert box is different from any other message or text on the screen. It is a pop-up that contains a message/text with an “OK” button. The user won't be able to do any task while an alert box is over the screen, and he/she clicks the “OK” button. So, it is not recommended, if not needed. So, let's have a look at what is an alert box and what are the different ways to use it.

The alert() is basically a method, which is used to show a pop-up box over the web page.

Syntax

There are two different syntaxes for showing the alert box. One of them is using the window's object.

window.alert("Alert box from the linuxhint");

But, we can use the alert() method without the window's object as well.

alert("Alert box from the linuxhint");

So, let's try both of the syntaxes.

Examples

First, let's try with the window's object.

window.alert("Alert box from the linuxhint");

And now, without the window's object.

alert("Alert box from the linuxhint");

You will witness that there is no difference in both of them.

The alert method doesn't only take the string to show the message. We can provide variable as well, and it worked perfectly fine,

var alertMessage = 'Alert Box using variable';
alert(alertMessage);

as you can see in the screenshot below that the message is displayed.

We have learned about providing a variable as well. What if we want to show the pop-up alert box on the screen at the click of a button? For example, we have got some information from the user, and after successfully saving the user's data on the server, we want to show a confirmation message that says “Added successfully”. So, we can simply show an alert box like this.

Or, if we are getting a confirmation message from the server, and we want to show the message on the base of the message we got. We can call the function on the button's onclick method

Then, later in the script, we can write the function in which we can show the alert message.

function alertFunc()
var alertMessage = 'Alert Box using function';
alert(alertMessage);

So, these are some of the different methods of using the alert() method.

Conclusion

In this article, we have learned about the javascript's built-in alert method to show pop-up over the browser's window. This article has explained the use of the alert method in a very easy, profound, and effective way that any beginner can understand and use. So, keep on learning, working, and getting experience in javascript with linuxhint.com to have a better grasp over it. Thank you so much!

How to Change Mouse and Touchpad Settings Using Xinput in Linux
Most Linux distributions ship with “libinput” library by default to handle input events on a system. It can process input events on both Wayland and X...
Remap your mouse buttons differently for different software with X-Mouse Button Control
Maybe you need a tool that could make your mouse's control change with every application that you use. If this is the case, you can try out an applica...
Microsoft Sculpt Touch Wireless Mouse Review
I recently read about the Microsoft Sculpt Touch wireless mouse and decided to buy it. After using it for a while, I decided to share my experience wi...