Home JavaScript Node JavaScript Node Definition
Definition and Usage A Node is the basic building block of the DOM tree. Elements, text, and comments are all nodes. Element and Text extend Node.
Every node can point to parentNode, childNodes, nextSibling, and similar links. Methods such as appendChild, removeChild, and cloneNode reshape the tree. nodeType tells you which kind of node you have.
This page teaches what Node is. Method, property, and event guides live under this section.
Everything visible in the tree is a Node. Even the spaces between tags can become text nodes. Prefer children on Element when you only want element children.
Syntax
JavaScript Syntax Common Node operations:
const p = document.createElement("p");
p.textContent = "Hello";
document.body.appendChild(p);
console.log(p.nodeType); // 1 (ELEMENT_NODE)
console.log(p.parentNode === document.body); Reference
Core Ideas Ideas every Node tutorial builds on:
Idea Example Remember nodeType node.nodeType1 element, 3 text, … parent node.parentNodeOwner in the tree children list node.childNodesIncludes text nodes append parent.appendChild(child)Moves if already elsewhere text node.textContentAll descendant text clone node.cloneNode(true)true copies descendants
Code
JavaScript Examples Three short programs that show how Node works.
1. nodeType 2. append 3. textContent Example 1 — Check nodeType Element nodes use type 1. Text nodes use type 3.
const el = document.createElement("div");
const text = document.createTextNode("Hi");
console.log(el.nodeType);
console.log(text.nodeType); How It Works createElement makes an element node (1).createTextNode makes a text node (3).Branch on nodeType when walking mixed trees. Example 2 — Append a child appendChild adds the node as the last child.
const parent = document.createElement("div");
const child = document.createElement("span");
parent.appendChild(child);
console.log(parent.childNodes.length); How It Works appendChild inserts child under parent.childNodes.length becomes 1.If the child was already in the tree, it is moved, not copied. Example 3 — Set textContent textContent replaces all descendant text in one assignment.
const p = document.createElement("p");
p.textContent = "Hello";
console.log(p.textContent); How It Works Assignment creates or replaces text under the node.Reading textContent returns that string. It does not parse HTML—use carefully with user input. Continue
Next: Node Guides Continue with appendChild and the other tree methods.
Look up Why appendChild() Add a child node removeChild()Remove a child cloneNode()Copy a node textContentRead or replace text
Full lists: appendChild() .
Frequently Asked Questions What is a Node in the DOM? A Node is any item in the document tree: elements, text, comments, and the document itself. Element and Text are specialized node types.
What is nodeType for? It is a number that identifies the kind of node. 1 is an element, 3 is text. Use it when walking mixed child lists.
childNodes vs children? childNodes includes text and comment nodes. children (on Element) lists only element children.
Does appendChild copy or move? It moves. If the node already has a parent, it is removed from the old place and inserted in the new one.
Where should I go next? Open appendChild(), then Element and Text for specialized node APIs.
Open appendChild() Then explore removeChild, cloneNode, and node properties.
appendChild() → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion