How to handle events in javascript?

Example:


  document.getElementById("btn").addEventListener("click", function() {
      alert("Button was clicked!");
  });
  

Solution:

Events in JavaScript allow you to execute code based on certain actions, like clicks. The `addEventListener` method is used to attach an event handler to an element.

Beginner's Guide to JavaScript