Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Node — a DOM node

Node is the shared interface for DOM nodes, including elements and text. It provides tree relationships, node-type inspection, and operations for changing a node's children.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

node.contains(other)
node.firstChild
node.parentElement
node.insertBefore(newNode, referenceNode)
node.normalize()
node.parentNode
node.isConnected
node.nodeType
node.nodeValue
node.childNodes
Node.TEXT_NODE
Node.ELEMENT_NODE

Node represents a DOM node, not just an element. nodeType distinguishes, among others, Node.ELEMENT_NODE (1) and Node.TEXT_NODE (3). nodeValue exposes text or comment data and attribute values, but returns null for an element; assigning it on an element does nothing.

childNodes is a live NodeList of all direct children, including whitespace text and comments. Tree changes update the collection. firstChild returns the first child of any type or null. parentNode returns the parent or null; parentElement returns that parent only if it is an element, otherwise null.

contains(other) tests whether other is the same node or a descendant; it returns false for null. It does not cross a shadow-tree boundary. isConnected indicates connection to a document, including through a shadow tree, rather than on-screen visibility.

insertBefore(newNode, referenceNode) returns newNode. If newNode is a DocumentFragment, the method inserts the fragment’s children instead of the fragment itself and returns the same, now-empty fragment. Otherwise, it inserts newNode itself. The reference must be a direct child; explicit null means the end of the list. An existing node is moved, not copied. An invalid reference parent causes NotFoundError, and an invalid hierarchy causes HierarchyRequestError. normalize() removes empty ordinary Text nodes in the subtree and merges adjacent ordinary Text nodes, retaining the first nonempty node in each merged run. It neither removes nor merges CDATASection nodes, even when empty; they separate runs of ordinary Text nodes. It does not strip whitespace from nonempty text.

Run the example in a browser with a DOM. Using Node 24 in a repository does not itself establish support for this browser API.

Reference identifiers covered by this entry: Node.contains, Node.firstChild, Node.parentElement, Node.insertBefore, Node.normalize, Node.parentNode, Node.isConnected, Node.nodeType, Node.nodeValue, Node.childNodes.

Examples

(() => {
  const root = document.createElement('div');
  const text = document.createTextNode('A');
  root.insertBefore(text, null);
  root.insertBefore(document.createTextNode('B'), null);
  const children = root.childNodes;

  console.log(children.length);
  console.log(root.nodeType === Node.ELEMENT_NODE);
  console.log(text.nodeType === Node.TEXT_NODE);
  console.log(text.parentNode === root, text.parentElement === root);
  console.log(root.contains(text), root.contains(root), root.contains(null));
  console.log(root.isConnected);

  root.normalize();
  console.log(children.length, root.firstChild.nodeValue);
  console.log(root.nodeValue === null);
})();

Result

2
true
true
true true
true true false
false
1 AB
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources