Alternative Encodings Beyond UTF-8
You'll learn how to use TextDecoder with legacy character encodings when working with older systems or regional content that doesn't use UTF-8.
While UTF-8 has become the web standard, you'll occasionally encounter binary data encoded in older formats like ISO-8859-1 (Latin-1) or Windows-1252. These single-byte encodings were common before Unicode dominance, especially in Western European systems. TextDecoder supports these legacy formats to ensure your code can still read them.
Specifying an Encoding
When creating a TextDecoder, you pass the encoding name as the first argument. If omitted, it defaults to 'utf-8', but you can specify alternatives:
// Default UTF-8
const utf8Decoder = new TextDecoder();
// Latin-1 (ISO-8859-1) decoder
const latin1Decoder = new TextDecoder('iso-8859-1');
// Windows-1252 decoder
const win1252Decoder = new TextDecoder('windows-1252');
When You Need This
Legacy encodings become necessary when reading files or responses from older systems. For example, Windows-1252 includes special characters like the Euro symbol (€) and "smart quotes" that ISO-8859-1 lacks:
// Bytes that represent special Windows-1252 characters
const bytes = new Uint8Array([0x80, 0x93, 0x94]); // €, ", "
const win1252 = new TextDecoder('windows-1252');
console.log(win1252.decode(bytes)); // "€""
const latin1 = new TextDecoder('iso-8859-1');
console.log(latin1.decode(bytes)); // Different characters (control codes)
Using the wrong decoder produces garbled text—like reading Spanish text through a French dictionary.
Key Takeaway: TextDecoder supports legacy encodings beyond UTF-8 by passing encoding names like 'iso-8859-1' or 'windows-1252' to its constructor, enabling you to correctly decode binary data from older systems.