Document

document.createTextNode

Creates a new Text node with the specified string as its content

Syntax

JavaScript
document.createTextNode(data)

Parameters

ParameterTypeDescription
datastringA string containing the text to put in the text node

Return Value

A new Text node

Examples

Basic Usage
const text = document.createTextNode('Hello World')
document.body.appendChild(text)
Practical Example
const span = document.createElement('span')
const label = document.createTextNode('Status: ')
span.appendChild(label)
span.appendChild(document.createTextNode('Active'))
Advanced Usage
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

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.