6 Simple and beautiful JavaScript alert with demos and code

Purpose of alerts

The alerts of JavaScript are used to notify users for certain functions, warning, a dangerous action etc. The JavaScript alerts are also used by web developers to debug or find some problem or track the values of variables etc.  

In this tutorial, I will show you how to create simple alerts in JavaScript. I will also show you fancy style alerts of different types like confirm, prompt etc. by using the third party JavaScript plug-ins.

A simple alert of JavaScript can be created quite easily, as shown below:

 

A demo to create and trigger simple alert

In this demo, a simple alert is created by using the alert keyword. As you click the button, the alert will be shown. See the demo and code online:

JavaScript alert simple

See online demo and code

An alert with a variable value

You can use variables inside the JavaScript alert dialogues. Simply place a variable name in place of string in double quotes and the alert will show the variable value.

This is a useful feature for developers as well for debugging applications (apart from other option, of course).

See a demo online:

See online demo and code

You see, I simply used variable name inside the alert without double quotes:

 

Similarly, you may concatenate a variable with a string by using operator as shown below:

alert(“Alert with concatenation: ” + alertstr);

Creating fancy alerts

All the alerts in above examples are using the default browser-based style that is considered boring and old-fashioned.

Unfortunately, this is what JavaScript comes up with. In order to use beautifully designed alert boxes or dialogue, you can use third party JavaScript solutions. For that, you simply need to include the JS libraries provided by alert developers into your web pages or follow their guidelines.

I will show you demos of fancy JavaScript alerts by using plug-ins. You may also see the steps to set up that plug-in for your web page after the demos.

A basic alert demo by using a plug-in

In this demo, I have used a plug-in, SweetAlert, which is available at the Github website. This plug-in is quite simple to use. First have a look at a few demos of different alerts by using it.

See a basic alert demo online by using SweetAlert:

JavaScript alert

See online demo and code

You can see, instead of using “alert” keyword in the <script> section, I just used “swal” to create a basic alert with a message and “Ok” button to close it.

An alert with success and animation

This JavaScript-based alert can be useful for some successful action by the user. For example, creating an account or subscribing for updates etc.

See a demo and code online for this alert:

JavaScript alert success

See online demo and code

The complete JS function code is:

 

This function is called at the click event of the button.

You may show this alert after filling the form by the user and upon successful entry into the database.

A JavaScript confirm dialogue example

You may use this alert type for giving a warning to the user before proceeding to some critical action like “deleting an account permanently” or deleting a file etc.

See the demo online with code:

JavaScript confirm alert

See online demo and code

You can see, a user is presented with a confirmation alert with Red button to proceed or cancel the operation. In each case, the related message is shown in next alert with an Ok button to close the alert. You may perform an action there rather showing alerts.

A prompt alert example

In this demo, a prompt alert is shown to take the user input before proceeding. See the demo online:

JavaScript prompt alert

See online demo and code

Setting up this cool plug-in

In order to use SweetAlert plug-in in your website, you simply need to include two dependency files in the <head> section of your webpage:

 <script src=” sweetalert-dev.js”></script>

<link rel=”stylesheet” href=” sweetalert.css”>

Get the files from the GitHub website. You may place these files in folders as per your project specifications. The downloaded package contains many other files but only two files are required as mentioned earlier and used in demos.