The Text() constructor builds a DOM text node. Learn optional string content, how it compares to document.createTextNode(), how to insert it with appendChild(), and how to read or update the text—with five examples and try-it labs.
01
Create
new Text()
02
Returns
Text node
03
Argument
optional string
04
Default
empty string
05
Twin
createTextNode
06
Status
Baseline widely
Fundamentals
Introduction
In the DOM, the letters you see inside an element are often stored in a Text node—not as a property of the element alone. When the browser parses <p>Hello</p>, it creates an element and a child text node whose data is "Hello".
new Text("Test") lets you build that kind of node yourself, then attach it with appendChild, insertBefore, or similar APIs.
JavaScript
const text = new Text("Test");
console.log(text.data); // "Test"
💡
Beginner tip
Creating a Text node does not show anything on screen. You must insert it into a parent that is already (or later becomes) in the document.
Concept
Understanding the Text() Constructor
MDN: the Text() constructor returns a new Text object with the optional string given in the parameter as its textual content.
Constructor — call with new Text(...).
Optional string — the node’s initial text.
Empty default — no argument means "".
CharacterData family — Text inherits data APIs (like data, length).
Textual content of the new text node. Omit for an empty node.
Return value
A new Text object whose content is the string parameter, or the empty string if no parameter was given.
Compare
🔄 new Text() vs createTextNode()
Both produce a Text node. Choose either style—know both so you can read older tutorials that only show document.createTextNode.
JavaScript
const a = new Text("Hello");
const b = document.createTextNode("Hello");
console.log(a.data === b.data); // true
console.log(a.nodeType === Node.TEXT_NODE); // true
console.log(b.nodeType === 3); // true
✅
When to prefer which
Use new Text() when you want a clear constructor style. Use document.createTextNode() when matching Document factory patterns already in a codebase. For setting lots of text on an element, often element.textContent = "..." is simpler than building nodes by hand.
Properties
📄 Reading and Updating Text
Property
What beginners use it for
data
CharacterData string stored in the text node
nodeValue
Same string via the Node API
textContent
Also the text for a Text node
length
Number of characters in data
nodeName
Always "#text" for text nodes
nodeType
3 / Node.TEXT_NODE
Cheat Sheet
⚡ Quick Reference
Goal
Code
Create with text
new Text("Test")
Empty text node
new Text()
Read content
text.data or text.nodeValue
Insert into DOM
parent.appendChild(text)
Classic twin
document.createTextNode("Test")
MDN status
Baseline Widely available (Oct 2017)
Snapshot
🔍 At a Glance
Four facts about new Text().
Kind
constructor
Web API
Returns
Text
DOM text node
Arg
string?
optional
Status
baseline
Widely available
Hands-On
Examples Gallery
Examples follow MDN Text() and everyday DOM patterns for inserting and inspecting text nodes.
📚 Getting Started
Create Text nodes and inspect their content.
Example 1 — MDN-Style new Text("Test")
Build a text node with an initial string.
JavaScript
const text = new Text("Test");
console.log(text.data);
console.log(text.nodeName);
console.log(text.nodeType);
console.log(text instanceof Text);
Per MDN, no parameter means empty content. You can still assign empty.data = "later" before or after inserting the node.
📈 Insert, Compare & Update
Use Text nodes with the rest of the DOM API.
Example 3 — Append to an Element
Creating is step one; appending makes the text visible.
JavaScript
const p = document.createElement("p");
const text = new Text("CodeToFun");
p.appendChild(text);
console.log(p.textContent);
console.log(p.firstChild.nodeName);
console.log(p.childNodes.length);
The Text() constructor is Baseline Widely available across modern browsers (MDN: since October 2017). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Text() constructor
Create DOM text nodes with an optional string — pair with appendChild to show them.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerPrefer createTextNode on very old IE
Limited
Text()Excellent
Bottom line: Use new Text(string) or document.createTextNode(string); both create Text nodes. Prefer element.textContent for simple whole-element text updates.
Wrap Up
Conclusion
new Text(string) builds a DOM text node with optional content. Append it to show text, or use createTextNode / textContent when that fits your code better.
Remember to appendChild (or insert) before expecting visible text
Use data / nodeValue to update an existing text node
Prefer textContent when replacing an element’s entire text
Pick one creation style per file (new Text or createTextNode)
Call normalize() if you create many adjacent text siblings
❌ Don’t
Expect new Text("Hi") alone to change the page
Confuse Text nodes with HTMLElement nodes
Use innerHTML just to add plain text (XSS risk if untrusted)
Assume every child of an element is an Element—often it is #text
Forget empty new Text() is valid and useful as a placeholder
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Text()
Construct text nodes, then insert them.
5
Core concepts
🖱️01
Constructor
new Text(string?)
API
📄02
Returns Text
nodeType 3
Result
🔄03
Twin
createTextNode
Alt
📤04
Insert
appendChild
DOM
🎯05
Baseline
widely available
Status
❓ Frequently Asked Questions
new Text() or new Text(string) returns a new Text (DOM text) node. If you pass a string, that becomes the node’s textual content; if you omit it, the content is the empty string.
No. MDN marks the Text() constructor as Baseline Widely available (since October 2017). It is not Deprecated, Experimental, or Non-standard.
Both create Text nodes. document.createTextNode(data) is the classic Document method; new Text(string) is the modern constructor. For everyday code either is fine—many tutorials still show createTextNode.
No. Creating a Text node only builds an object. Append it with parent.appendChild(text) (or similar) before it appears in the document.
Text nodes have nodeType 3 (Node.TEXT_NODE) and nodeName "#text". You can also read the string via data, nodeValue, or textContent.
Yes. Assign to text.data, text.nodeValue, or text.textContent after the node exists—including after it is in the DOM.
Did you know?
Whitespace in HTML often becomes text nodes too. That is why element.childNodes can list more nodes than element.children—the latter lists only element children, skipping #text.