In the DOM (Document Object Model), a node is the basic building block of the page structure. Everything in an HTML document—elements, text, comments, attributes, and even the document itself—is represented as a node in a hierarchical tree.
By working with nodes, JavaScript can traverse, inspect, create, and modify parts of a webpage, making your applications dynamic and interactive.
<div>, <p>, <span>, etc.nodeType – A numeric value that tells you what kind of node you’re dealing with.Some core properties available on DOM nodes:
nodeType – Returns a number representing the type of the node (e.g., 1 for elements, 3 for text).nodeName – The node’s name (e.g., tag name for elements, #text for text nodes).nodeValue – The content of text or comment nodes; for element nodes it is always null.parentNode – The node’s direct parent in the DOM tree.childNodes – A live collection of all child nodes, including text nodes and comments.firstChild / lastChild – References to the first and last child node.Common numeric values for nodeType are also exposed as constants on the global Node object:
| Constant | Value | Description |
|---|---|---|
Node.ELEMENT_NODE |
1 | Element node |
Node.TEXT_NODE |
3 | Text node |
Node.COMMENT_NODE |
8 | Comment node |
Node.DOCUMENT_NODE |
9 | Document node |
nodeType and nodeName.childNodes to understand everything (including comments and whitespace) inside a container.
<div id="test">Hello<!-- Comment --></div>
<script>
const test = document.getElementById("test");
console.log(test.nodeType); // 1 (Element Node)
console.log(test.firstChild.nodeType); // 3 (Text Node)
console.log(test.lastChild.nodeType); // 8 (Comment Node)
</script>
<div id="container">Hello
<!-- comment -->
<span>World</span>
</div>
<script>
const container = document.getElementById("container");
container.childNodes.forEach(node => {
console.log(node.nodeType, node.nodeName, node.nodeValue);
});
</script>
const textNode = document.createTextNode("Hello");
document.body.appendChild(textNode);
textNode.nodeValue = "Hello JS!";
const p = document.createElement("p");
p.appendChild(document.createTextNode("New paragraph."));
document.body.appendChild(p);
In the first example, logging test.nodeType prints 1, meaning the <div> is an element node. Its firstChild is a text node (3), and its lastChild is the comment node (8).
In the second example, iterating over container.childNodes prints each child’s nodeType, nodeName, and nodeValue. You’ll see:
"Hello".<!-- comment -->.<span>World</span>.In the text-node example, updating nodeValue from "Hello" to "Hello JS!" immediately updates what you see on the page. In the final example, we build a new <p> element, attach a text node to it, and append it to the document, dynamically adding content to the DOM.
nodeType to safely identify the kind of node you’re working with.childNodes includes text and comment nodes; use children if you only care about element nodes.document.createElement() and document.createTextNode() for precise control.null) before accessing its properties.nodeValue on element nodes always returns null.document.createComment() when you need to add comments dynamically to the DOM.nodeType and nodeName of its child nodes.createTextNode().nodeValue of a text node and observe the update in the browser.document.createComment() and append it to document.body.childNodes and children on a complex element that contains text, comments, and other elements.firstChild and lastChild on different elements to see what they reference.