Utilities
TextDecoder.decode()
Decodes bytes using the selected encoding. With fatal true, invalid byte sequences throw instead of being silently replaced. Use stream true for incomplete chunk boundaries when decoding a stream. Tested on Node.js 22.16.0. Example output (one line per entry): ["a", "TypeError"].
Formal Syntax & Structure
nodejs syntaxnodejs
decoder.decode([input[, options]])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| input | ArrayBuffer | TypedArray | DataView | Bytes to decode. |
| options | object | Streaming option. |
Return Value
string
Decoded text.
Working Example
Example Snippetnodejs
import { TextDecoder } from 'node:util';const d = new TextDecoder('utf-8', { fatal: true });console.log(d.decode(Uint8Array.of(0x61)));try { d.decode(Uint8Array.of(0xff));}catch (e) { console.log(e.name);}