TextDecoder.prototype.decode
Decodes a buffer of bytes into a string using the specified character encoding
Syntax
new TextDecoder(encoding?).decode(buffer?)Parameters
| Parameter | Type | Description |
|---|---|---|
| buffer | ArrayBuffer | TypedArray | DataView | The buffer to decode |
Return Value
A string containing the decoded text
Examples
const decoder = new TextDecoder()
const bytes = new Uint8Array([72, 101, 108, 108, 111])
console.log(decoder.decode(bytes)) // 'Hello'const decoder = new TextDecoder('utf-8')
const response = await fetch('/api/data')
const buffer = await response.arrayBuffer()
const text = decoder.decode(buffer)
console.log(text)function bytesToString(bytes: Uint8Array): string {
return new TextDecoder().decode(bytes)
}Understanding TextDecoder.prototype.decode
The TextDecoder.prototype.decode method in JavaScript decodes a buffer of bytes into a string using the specified character encoding. It belongs to the TextDecoder object and is one of the most widely used methods for working with textdecoder values in modern JavaScript and TypeScript applications.
The method signature is new TextDecoder(encoding?).decode(buffer?). It accepts 1 parameter: buffer. When called, it returns a string containing the decoded text. Understanding when and how to use decode() helps you write more expressive, readable code.
Common use cases for TextDecoder.prototype.decode include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like textencoder-encode, fetch-response-text, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for TextDecoder.prototype.decode 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
Related Tools
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.