DOM Node Types
DOM nodes have distinct types, including elements, text, comments, and the document itself, and each type has different properties and behavior. Knowing the node type helps you choose the correct DOM APIs and avoid assumptions that every node is an element.
The DOM represents an HTML or XML document as a tree of nodes. A node's type describes what that node represents, such as an HTML element, text content, comment, or the document root.
For frontend work, `Element` nodes are especially important because they represent tags such as `<div>`, `<p>`, and `<button>`. Elements provide APIs such as `classList`, `attributes`, and `querySelector()`. Text inside an element is represented separately by a `Text` node.
A `Text` node contains character data and does not have element-specific properties such as `classList`. Comments are represented by `Comment` nodes and can appear in the DOM tree even though they are not rendered as visible page content.
The document itself is represented by a `Document` node. Other node types also exist, including document fragments and document types. Code that traverses a DOM tree should therefore avoid assuming that every child returned by `childNodes` is an element.
JavaScript exposes node-type information through `node.nodeType`. Common constants include `Node.ELEMENT_NODE`, `Node.TEXT_NODE`, and `Node.COMMENT_NODE`. Using these constants makes code clearer than relying on numeric values such as `1`, `3`, or `8`.
Key points
- `Element`, `Text`, `Comment`, and `Document` are different DOM node types.
- `nodeType` identifies the kind of node.
- `childNodes` can contain text and comment nodes, not just elements.
- Element-specific APIs such as `classList` are not available on every node.
- Prefer named constants such as `Node.ELEMENT_NODE` over raw numeric node-type values.
Interview questions
What are the main DOM node types you should know?
Common types include `Document`, `Element`, `Text`, and `Comment`. An `Element` represents an HTML tag, while a `Text` node stores text inside the tree. A `Comment` represents an HTML comment, and `Document` represents the document as a whole. There are additional node types, but these are common in everyday DOM work.
What is the difference between `children` and `childNodes`?
`children` returns the element children of a node. `childNodes` returns all child nodes, which can include elements, text nodes, and comments. For example, whitespace in HTML can produce text nodes, so `childNodes` may contain more entries than expected. The choice depends on whether the code needs only elements or every node in the DOM tree.
How do you check whether a DOM node is an element?
A common approach is to compare `node.nodeType` with `Node.ELEMENT_NODE`. You can also use `node instanceof Element` when the relevant DOM realm makes that check appropriate. The node-type check directly expresses the DOM category being tested. The important point is not to assume an arbitrary `Node` is an element.
Why can iterating over `childNodes` cause bugs?
`childNodes` includes more than element nodes, so an iteration can encounter text or comment nodes. Text nodes are especially common because whitespace between HTML elements can become text nodes. If code assumes every child has properties such as `classList`, it can fail. Code should either filter for elements or use `children` when element-only traversal is intended.
What is the relationship between `Node`, `Element`, and `HTMLElement`?
`Node` is the broad DOM abstraction implemented by multiple node categories. `Element` represents nodes corresponding to elements in the document tree, while `HTMLElement` is a more specific interface for HTML elements and provides HTML-specific behavior. Thus, an HTML button is an `HTMLElement`, an `Element`, and a `Node`. Understanding this hierarchy helps determine which APIs are guaranteed to exist on a value.