Text encoding
UTF-8 only. Both constructors ignore their arguments — there is no
latin1, utf-16, or fatal mode.
TextEncoder
There is no encodeInto — write into a target buffer yourself:
TextDecoder
Invalid bytes are replaced, never fatal
Malformed UTF-8 becomes U+FFFD (``) — byte by byte:
There is no fatal: true mode: new TextDecoder("utf-8", { fatal: true })
parses but the options are ignored.
Wrong input type throws
Strings are rejected — convert with encode() first. ArrayBuffer itself
is accepted:
Constructor labels are ignored
Patterns
JSON over bytes
Base64 → bytes → string
Gotchas
- Both classes are UTF-8 only; labels don't change behaviour.
decode()is lossy by design — invalid sequences become ``. If you need to detect corruption, validate the bytes yourself (TextDecoderwon't tell you).encode()allocates a freshUint8Arrayper call — don't call it in a hot loop with large strings.- No streaming:
decode(chunk, { stream: true })ignores the option and may mangle multi-byte characters split across chunks.
See also
fetch— reading response bodies- Blob and FormData
- Compatibility
