TextDecoder and TextEncoder in JavaScript
JavaScript provides TextDecoder and TextEncoder to work with text encoding and decoding, especially when handling binary data (e.g., ArrayBuffer
, Uint8Array
).
1️⃣ TextEncoder – Encoding Text to Binary
The TextEncoder
converts text (string) into UTF-8 encoded bytes (Uint8Array
).
Example: Convert String to UTF-8 Bytes
Each number in the Uint8Array
represents the UTF-8 byte value of the corresponding character.
2️⃣ TextDecoder – Decoding Binary to Text
The TextDecoder
converts UTF-8 encoded bytes (Uint8Array
, ArrayBuffer
) back to a string.
Example: Convert UTF-8 Bytes Back to String
3️⃣ Working with Different Encodings
By default, it TextEncoder
uses UTF-8 (which covers most cases).
However, TextDecoder
supports multiple encodings.
Example: Using Different Encodings
For other encodings like ISO-8859-1, Shift_JIS, Windows-1252, check browser compatibility as TextDecoder
might not support all encodings.
4️⃣ Handling Streams with TextDecoder
If you are working with large amounts of data (e.g., reading a file or a network stream), use streaming decoding.
Using { stream: true }
ensures the decoder keeps track of partial characters.
5️⃣ Example: Encoding & Decoding JSON Data
6️⃣ Use Cases of TextEncoder
& TextDecoder
✅ Working with binary data (e.g., WebSockets, Fetch API, IndexedDB)
✅ Reading and writing files (e.g., FileReader
, Blob
)
✅ Handling Unicode text in buffers
✅ Encoding and decoding JSON for storage/transmission
Conclusion
TextEncoder
converts text → binary (Uint8Array
).TextDecoder
converts binary (Uint8Array
) → text.- UTF-8 is the default encoding, but
TextDecoder
supports others. - Used in file handling, network requests, and data processing.
Let me know if you need more details! š