Response

Response.prototype.text

Takes a Response stream and reads it to completion, returning the result as a string

Syntax

JavaScript
response.text()

Return Value

A Promise that resolves to a string

Examples

Basic Usage
const response = await fetch('/readme.txt')
const text = await response.text()
console.log(text)
Practical Example
async function fetchHTML(url: string): Promise<string> {
  const res = await fetch(url)
  if (!res.ok) throw new Error(`HTTP ${res.status}`)
  return res.text()
}
Advanced Usage
const res = await fetch('/api/health')
const body = await res.text()
try {
  const data = JSON.parse(body)
  console.log(data)
} catch {
  console.log('Plain text:', body)
}

Understanding Response.prototype.text

The Response.prototype.text method in JavaScript takes a Response stream and reads it to completion, returning the result as a string. It belongs to the Response object and is one of the most widely used methods for working with response values in modern JavaScript and TypeScript applications.

The method signature is response.text(). When called, it returns a promise that resolves to a string. Understanding when and how to use text() helps you write more expressive, readable code.

Common use cases for Response.prototype.text include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like fetch-response-json, fetch-response-blob, fetch-response-arraybuffer, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Response.prototype.text 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 Response Methods

Other methods in the Response object

Related Tools

More Response Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.