Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to use the alert() method in JavaScript ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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. 



Below is the implementation of the above approach:



<!DOCTYPE html>
<html lang="en">
      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;
    <div style="margin-left: 500px; margin-top: 300px;">
      <button style="font-size: 20px;" 
       class="btn" onclick="fun()">
       click me
      function fun(){
       alert("Welcome on gfg!");


My Personal Notes arrow_drop_up
Last Updated : 31 Mar, 2021
Like Article
Save Article
Similar Reads
Related Tutorials