document.createTextNode() is an instance method that creates a new Text node (see MDN Document: createTextNode()). Learn the data parameter, how Text nodes escape HTML characters, how to append them, how they differ from innerHTML, and five try-it labs.
01
Kind
Instance method
02
Args
data (string)
03
Returns
Text node
04
nodeType
3 (TEXT_NODE)
05
Safety
Escapes HTML
06
Status
Baseline
Fundamentals
Introduction
Almost every visible string on a web page lives inside a Text node — the characters between tags. When you write <p>Hello</p>, the word Hello is a Text node child of the paragraph.
document.createTextNode(data) builds that kind of node in JavaScript. MDN also notes an important safety tip: this method can be used to escape HTML characters. If the string contains < or &, they stay as text instead of becoming markup.
💡
Create → append
1) const text = document.createTextNode("Hello") 2) element.appendChild(text) (MDN pattern) 3) The string appears as plain text in the page
Document.createTextNode() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createTextNode()
Create Text nodes for safe plain-text DOM content across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createTextNode()Wide
Bottom line: Use createTextNode(data) for plain text (and HTML-escaping). Append the Text node into an element to show it on the page.
Wrap Up
Conclusion
document.createTextNode(data) builds a Text node from a string. MDN highlights that it can escape HTML characters — ideal for inserting plain text safely. Create the node, append it, and you have reliable text in the DOM.
Use Text nodes for untrusted or user-provided strings (MDN escape tip)
Append with appendChild / append
Pair with createElement when building structure
Check nodeType === Node.TEXT_NODE while learning
Prefer textContent when you only need to replace all text
❌ Don’t
Put untrusted HTML into innerHTML when Text would do
Expect a detached Text node to appear without appending it
Confuse Text nodes with Comment or Element nodes
Assume createTextNode parses tags (it does not)
Forget spaces when concatenating multiple Text appends
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createTextNode()
Create plain-text DOM nodes safely.
5
Core concepts
📝01
Returns
Text node
MDN
📄02
Arg
data
string
🛡03
Escapes
HTML chars
MDN
➕04
Attach
appendChild
MDN
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.createTextNode() creates a new Text node. Pass a string (data) and you get a Text node you can append into the DOM.
No. MDN marks Document.createTextNode() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A Text node (MDN). Read text.data, text.nodeValue, or text.textContent for the string.
MDN notes this method can be used to escape HTML characters. Characters like < and & stay as plain text — they are not parsed as markup (unlike innerHTML).
Node.TEXT_NODE, which equals 3. You can also use text instanceof Text.
Use createTextNode when you need an actual Text node to insert, move, or split. Assigning element.textContent is shorter when you only want to replace all text inside an element.
Did you know?
You can also write new Text("Hello") in modern browsers. The classic document.createTextNode("Hello") factory is what MDN documents and what you will see most often in tutorials and older codebases.