Explain JavaScript text alert

The JavaScript alert() function is used to display a popup message to the user.

Following is the code for the JavaScript alert() function −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
<h1>Javascript text alert</h1>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to trigger the alert popup</h3>
   let BtnEle = document.querySelector(".Btn");
   BtnEle.addEventListener("click", () => {
      alert("You pressed the button");


The above code will produce the following output −

On clicking the ‘CLICK HERE’ button −