How to use the alert() method in JavaScript ? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to use the alert() method in JavaScript. The alert() method is used to show an alert box on the browser window with some message or warning. We can use it as a message or as a warning for the user. Approach: To show an alert on the browser window, we make a button. When we click on the button, a click event listener is called that calls a function. In that function, we use the alert() method that contains a message for the user. When we click the button, the alert window will pop up on the browser window that contains a message or warning with a button. Then you have to click the OK button so that the alert box can be closed.Syntax:alert(message/warning);Below is the implementation of the above approach: Example: This example shows the use of the above-explained approach. HTML <!DOCTYPE html> <html lang="en"> <head> <style> body { /* path of the image */ background-image: url(gfg_complete_logo_2x-min.png); /* Image is always centered*/ background-position: center center; /* set the image fixed to the viewport */ background-attachment: fixed; /* Not repeat image */ background-repeat: no-repeat; /* Set background size auto */ background-size: auto; } </style> </head> <body> <div style="margin-left: 500px; margin-top: 300px;"> <button style="font-size: 20px;" class="btn" onclick="fun()"> click me </button> </div> <script> function fun() { alert("Welcome on gfg!"); } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Change the Color of the Alert Box in JavaScript ? S sachinchhipa44 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Methods Similar Reads How to show Image in an Alert Box using JavaScript ? Adding images in JavaScript alerts can make messages more eye-catching and easier to understand. It's a way to attract your attention and make the alerts more attractive. We will show the steps on how to put images in our JavaScript alerts and make your notifications clearer and more engaging for us 3 min read How to Change the Color of the Alert Box in JavaScript ? An alert box is a dialogue box that pops up on the screen to provide information to the user. It contains a message and an OK button, which allow users to acknowledge the message and close the box. Alert boxes are commonly used in web development to display important messages, warnings, or notificat 5 min read How to Create an Alert in JavaScript ? The alert() method in JavaScript displays an alert box with a message and an OK button. It's used when you want information to come through to the user, providing immediate notifications or prompts for user interaction during program execution. Note: Alert boxes interrupt user interaction, shifting 1 min read How to Print a String in JavaScript ? In JavaScript, printing a string means displaying the contents of a string variable or literal to the console, webpage, or any other output medium. Strings are a fundamental data type in JavaScript, representing sequences of characters enclosed within single ('') or double ("") quotes. Table of Cont 2 min read How to trigger events in JavaScript ? JavaScript is a high-level, interpreted, dynamically typed client-side scripting language. While HTML is static and defines the structure of a web page, JavaScript adds interactivity and functionality to HTML elements. This interaction is facilitated through events, which are actions or occurrences 2 min read How to Edit a JavaScript Alert Box Title ? We can't directly modify the title of the alert box because the title is controlled by the browser and cannot be changed by JavaScript. However, we can create a custom alert box. Table of Content Using a Custom Alert Box FunctionUsing SweetAlert LibraryUsing a Custom Alert Box FunctionIn this approa 2 min read Like