JavaScript - Events

JavaScript events are actions or occurrences that happen in the browser, such as a user clicking a button, a page finishing loading, or an element being updated. JavaScript can respond to events and perform actions based on them.


<button id="myBtn">Click me</button>


  let button = document.querySelector("#myBtn");


  button.addEventListener("click", function () {

    alert("Button was clicked");



In the above example, the click event of the button element is being listened for using the addEventListener() method. When the button is clicked, the anonymous function passed to addEventListener() will be executed, displaying an alert message.

JavaScript events are an important aspect of interactivity in web pages, allowing developers to create dynamic and responsive user interfaces.