What are events in JavaScript?

JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page.

When the page loads, it is called an event. When the user clicks a button, that click too is an event. Other examples include events like pressing any key, closing a window, resizing a window, etc. Events are a part of the Document Object Model (DOM) Level 3 and every HTML element contains a set of events which can trigger JavaScript Code.

Let’s see an example of onclick event in JavaScript, which is the most frequently used event type which occurs when a user clicks the left button of his mouse.


You can put your validation, warning etc., against this event type.

            function sayHello() {
               alert("Hello World")
      <p>Click the following button and see result</p>

         <input type = "button" onclick = "sayHello()" value = "Say Hello" />

Updated on: 17-Jan-2020


Kickstart Your Career

Get certified by completing the course

Get Started