Check a JavaScript Object is a DOM Object
Last Updated :
14 Oct, 2025
In JavaScript, working with DOM (Document Object Model) elements is a common task. However, sometimes you may need to verify whether a given object is a DOM element or just a plain JavaScript object. Checking this correctly helps prevent runtime errors when performing DOM-specific operations.
Element
In HTML DOM, Element is the general base class for all objects. An Element object represents all HTML elements.
[Approach 1]: Using instanceof operator
To check if a JavaScript object is a DOM object, use the instanceof operator. This operator determines whether the object is an instance of the Element class. If true, it indicates the object is part of the DOM, enabling dynamic HTML manipulation.
Parameters:
- Object: It stores the Object which need to be tested.
- ObjectType: It stores the Object Type to be tested against.
html
<!DOCTYPE HTML>
<html>
<head>
<title>
How to check a JavaScript Object is a DOM Object?
</title>
</head>
<body>
<div id="div1"></div>
<div id="nonElem"></div>
<script>
// Check if object is a DOM Element
function isDOM(Obj) {
return Obj instanceof Element;
}
// Get elements
let div = document.getElementById('div1');
let nonElem = document.getElementById('nonElem');
// Non-DOM object
let x = 1;
// Check and update innerHTML
if (isDOM(div))
div.innerHTML = "Div is detected as a DOM Object";
if (!isDOM(x))
nonElem.innerHTML = "x is detected as a non-DOM Object";
</script>
</body>
</html>
Output:
Div is detected as a DOM Object
x is detected as a non-DOM Object
Syntax
Object instanceof ObjectType
[Approach 2]: Using nodeType
Property
Every DOM node has a nodeType
property that identifies the type of node. For element nodes, the value is 1
:
JavaScript
let element = document.querySelector('div');
if (element && element.nodeType === 1) {
console.log("It's a DOM element");
} else {
console.log("Not a DOM element");
}
Output:
It's a DOM element
Explore
HTML Basics
Structure & Elements
Lists
Visuals & Media
Layouts & Designs
Projects & Advanced Topics
Tutorial References