When working with the DOM (Document Object Model), we often encounter three common types of nodes:
- Element Node: Corresponds to HTML tags (e.g.,
<div>
,<p>
, etc.) - Text Node: Represents the text content within an element.
- Comment Node: Represents comments in HTML (e.g.,
<!-- comment -->
).
These three types of nodes differ in behavior and properties. Here’s a summary:
Using childNodes
vs children
#
childNodes
: Returns all types of child nodes—including element nodes, text nodes, and comment nodes. The return type is aNodeList
.children
: Returns only element nodes (Element Node
). The return type is anHTMLCollection
.
Only Element Nodes have the children
property.
Text Nodes and Comment Nodes do not have this property and can only use childNodes
(which usually returns an empty list, since they typically have no children).