latin1, utf-16, or fatal mode.
TextEncoder
encodeInto — write into a target buffer yourself:
TextDecoder
Invalid bytes are replaced, never fatal
Malformed UTF-8 becomes U+FFFD (“) — byte by byte:fatal: true mode: new TextDecoder("utf-8", { fatal: true })
parses but the options are ignored.
Wrong input type throws
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