document.createTextNode
Creates a new Text node with the specified string as its content
Syntax
document.createTextNode(data)Parameters
| Parameter | Type | Description |
|---|---|---|
| data | string | A string containing the text to put in the text node |
Return Value
A new Text node
Examples
const text = document.createTextNode('Hello World')
document.body.appendChild(text)const span = document.createElement('span')
const label = document.createTextNode('Status: ')
span.appendChild(label)
span.appendChild(document.createTextNode('Active'))function safeInsert(parent: HTMLElement, content: string) {
const text = document.createTextNode(content)
parent.appendChild(text)
}Understanding document.createTextNode
The document.createTextNode method in JavaScript creates a new Text node with the specified string as its content. It belongs to the Document object and is one of the most widely used methods for working with document values in modern JavaScript and TypeScript applications.
The method signature is document.createTextNode(data). It accepts 1 parameter: data. When called, it returns a new text node. Understanding when and how to use createTextNode() helps you write more expressive, readable code.
Common use cases for document.createTextNode include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like dom-createelement, dom-appendchild, dom-textcontent, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for document.createTextNode is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
document.createElementCreates the HTML element specified by tagName, or an HTMLUnknownElement if tagName is not recognized
Element.prototype.appendChildAdds a node to the end of the list of children of a specified parent node
Element.prototype.textContentGets or sets the text content of the node and its descendants
More Document Methods
Other methods in the Document object
Related Tools
More Document Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.