How to determine if JavaScript object is an event?

In JavaScript, distinguishing between regular objects and event objects is important when handling user interactions and DOM events. An event object contains information about an event that occurred, such as mouse clicks or keyboard presses, while regular objects store data as properties and methods.

JavaScript events are actions that happen in the browser, like clicking a button or loading a page. When these events occur, JavaScript creates event objects that contain details about what happened. Let's explore two reliable methods to check if an object is an event.

Using the target Property

Event objects have a target property that references the element that triggered the event. We can use this to identify event objects.

Syntax

function isEvent(obj) {
   return obj && typeof obj === 'object' && 'target' in obj;
}

Example

This example demonstrates checking objects using the target property. We'll test both an actual event object and a regular string.

<html>
<body>
   <h3>Using the target Property</h3>
   <button id="testButton">Click Me</button>
   <p id="eventResult"></p>
   <p id="stringResult"></p>
   
   <script>
      function checkIfEvent(obj, resultId) {
         const hasTarget = obj && typeof obj === 'object' && 'target' in obj;
         const result = hasTarget ? "an event" : "not an event";
         document.getElementById(resultId).innerHTML = `Object is ${result}`;
      }
      
      // Test with button click event
      document.getElementById("testButton").addEventListener("click", function(event) {
         checkIfEvent(event, "eventResult");
      });
      
      // Test with string object
      checkIfEvent("hello world", "stringResult");
   </script>
</body>
</html>

Using the instanceof Operator

The instanceof operator checks if an object was created by a specific constructor. Event objects inherit from the Event constructor.

Syntax

obj instanceof Event  // Returns true if obj is an Event

Example

This example creates different types of objects and tests them using instanceof Event.

<html>
<body>
   <h3>Using instanceof Event</h3>
   <p id="eventTest"></p>
   <p id="stringTest"></p>
   <p id="objectTest"></p>
   
   <script>
      function testObject(obj, name, resultId) {
         const isEvent = obj instanceof Event;
         const result = isEvent ? "an event" : "not an event";
         document.getElementById(resultId).innerHTML = `${name} is ${result}`;
      }
      
      // Create and test different objects
      const clickEvent = new Event('click');
      const regularString = "hello";
      const regularObject = { name: "test" };
      
      testObject(clickEvent, "Click event", "eventTest");
      testObject(regularString, "String", "stringTest");
      testObject(regularObject, "Regular object", "objectTest");
   </script>
</body>
</html>

Comparison of Methods

Method Pros Cons Best Use Case
target Property Works with all event types May give false positives Event handlers and listeners
instanceof Event Most reliable and precise Only works with Event objects Programmatically created events

Conclusion

Use instanceof Event for the most reliable detection of event objects. The target property method works well in event handlers where you're certain you're dealing with events. Both approaches help distinguish between regular objects and event objects effectively.

Updated on: 2026-03-15T20:55:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements