JavaScript Text

Beginner
8 min read
Updated: Nov 2026
3 examples
Text nodes

Definition and Usage

A Text node holds character data in the DOM—the letters between tags. It is a Node with nodeType 3, not an Element.

Create one with document.createTextNode("Hello") or new Text("Hello"). splitText(offset) splits a node in two. wholeText joins adjacent text nodes’ contents for reading.

This page teaches what Text is. Constructor, methods, and properties live under this section.

textContent vs Text nodes.

Setting element.textContent is usually easier. Use Text nodes when you need split/merge control or precise tree edits.

JavaScript Syntax

Creating and reading a Text node:

JavaScript
const text = document.createTextNode("Hello");
console.log(text.nodeType); // 3
console.log(text.data);
p.appendChild(text);

Core Ideas

Ideas every Text tutorial builds on:

IdeaExampleRemember
nodeType 3text.nodeTypeIdentifies text nodes
Createdocument.createTextNode("Hi")Or new Text("Hi")
datatext.dataThe character string
splitTexttext.splitText(2)Breaks into two nodes
wholeTexttext.wholeTextAdjacent text combined
Not Elementno tagNameNo attributes or classList

JavaScript Examples

Three short programs that show how Text works.

Example 1 — Create a text node

createTextNode returns a Text instance.

JavaScript
const text = document.createTextNode("Hello");
console.log(text.data);
console.log(text.nodeType);

How It Works

  1. createTextNode("Hello") builds the node.
  2. data holds the characters.
  3. nodeType is 3.

Example 2 — Split at an offset

splitText divides one Text into two siblings.

JavaScript
const text = document.createTextNode("Hello");
const second = text.splitText(2);
console.log(text.data);
console.log(second.data);

How It Works

  1. splitText(2) cuts before index 2.
  2. The original node keeps "He".
  3. The returned node holds "llo".

Example 3 — Read wholeText

wholeText concatenates adjacent text nodes.

JavaScript
parent.appendChild(document.createTextNode("Hi"));
parent.appendChild(document.createTextNode("!"));
console.log(parent.firstChild.wholeText);

How It Works

  1. Two text nodes sit next to each other.
  2. wholeText on the first includes both.
  3. Useful after edits that leave split text nodes.

Next: Text Guides

Continue with the constructor and splitText.

Look upWhy
Constructornew Text(data)
splitText()Split at a character offset
wholeTextAdjacent text as one string
dataThe character contents

Full lists: Text constructor.

Frequently Asked Questions

A Text node stores characters in the DOM tree. It has nodeType 3 and no tag name or attributes.
When you need fine control such as splitText. For simple labels, element.textContent is enough.
It cuts the text at an index, keeps the left part in the original node, and returns a new Text node with the right part.
It returns the text of this node plus any adjacent text-node siblings, as one string.
Open the Text constructor, then Node and Document createTextNode.

Open the Text constructor

Then practice splitText and wholeText on small examples.

Text constructor →

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