Course contentsShow
← JavaScript
Lesson 2371 of 2,72348. Working with Binary DataFree lesson

Converting Between Formats

Transform Blobs to ArrayBuffers, text, or data URLs and create new Blobs from various sources.

Converting Between Formats

You'll learn how to transform Blobs into ArrayBuffers, text, or data URLs, and create new Blobs from various binary sources.

The Challenge of Binary Data Interchange

You've seen Blobs, ArrayBuffers, text, and data URLs as separate concepts. In real applications, you frequently need to convert between these formats. Maybe you receive an ArrayBuffer from fetch() but need a Blob for file processing. Or you have a Blob from a file input but need its text content. Understanding these conversions is essential for flexible binary data handling.

Blob to Other Formats

Modern Blob objects provide built-in methods for conversion:

const blob = new Blob(['Hello, world!'], { type: 'text/plain' });

// Blob to text
const text = await blob.text();
console.log(text); // "Hello, world!"

// Blob to ArrayBuffer
const buffer = await blob.arrayBuffer();
console.log(buffer.byteLength); // 13

// Blob to data URL
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = () => {
  console.log(reader.result); // "data:text/plain;base64,SGVsbG8sIHdvcmxkIQ=="
};

Creating Blobs from Other Formats

You can create Blobs from virtually any binary source:

// From ArrayBuffer
const buffer = new Uint8Array([72, 105]).buffer;
const blobFromBuffer = new Blob([buffer]);

// From text (automatically encoded to UTF-8)
const blobFromText = new Blob(['Some text']);

// From multiple sources mixed
const mixed = new Blob([buffer, ' and ', 'more text']);

Think of these conversions like currency exchange: the underlying value (your data) stays the same, but the format changes to suit different needs.

Key Takeaway: Blobs offer .text() and .arrayBuffer() methods for reading, while the Blob constructor accepts ArrayBuffers, strings, and other Blobs as input—enabling seamless format conversion across your binary data workflows.