JavaScript Node

Beginner
8 min read
Updated: Nov 2026
3 examples
DOM tree

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.

JavaScript Syntax

Common Node operations:

JavaScript
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);

Core Ideas

Ideas every Node tutorial builds on:

IdeaExampleRemember
nodeTypenode.nodeType1 element, 3 text, …
parentnode.parentNodeOwner in the tree
children listnode.childNodesIncludes text nodes
appendparent.appendChild(child)Moves if already elsewhere
textnode.textContentAll descendant text
clonenode.cloneNode(true)true copies descendants

JavaScript Examples

Three short programs that show how Node works.

Example 1 — Check nodeType

Element nodes use type 1. Text nodes use type 3.

JavaScript
const el = document.createElement("div");
const text = document.createTextNode("Hi");
console.log(el.nodeType);
console.log(text.nodeType);

How It Works

  1. createElement makes an element node (1).
  2. createTextNode makes a text node (3).
  3. Branch on nodeType when walking mixed trees.

Example 2 — Append a child

appendChild adds the node as the last child.

JavaScript
const parent = document.createElement("div");
const child = document.createElement("span");
parent.appendChild(child);
console.log(parent.childNodes.length);

How It Works

  1. appendChild inserts child under parent.
  2. childNodes.length becomes 1.
  3. 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.

JavaScript
const p = document.createElement("p");
p.textContent = "Hello";
console.log(p.textContent);

How It Works

  1. Assignment creates or replaces text under the node.
  2. Reading textContent returns that string.
  3. It does not parse HTML—use carefully with user input.

Next: Node Guides

Continue with appendChild and the other tree methods.

Look upWhy
appendChild()Add a child node
removeChild()Remove a child
cloneNode()Copy a node
textContentRead or replace text

Full lists: appendChild().

Frequently Asked Questions

A Node is any item in the document tree: elements, text, comments, and the document itself. Element and Text are specialized node types.
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 includes text and comment nodes. children (on Element) lists only element children.
It moves. If the node already has a parent, it is removed from the old place and inserted in the new one.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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