Creating Blobs
You'll learn how to instantiate Blob objects from various data sources using the Blob constructor.
What Is a Blob in Practice?
A Blob (Binary Large Object) represents raw binary data in JavaScript. Think of it as a container you can fill with different types of data—text, binary arrays, or even other Blobs—and seal it up for storage, downloading, or sending over the network.
The Blob Constructor
You create a Blob using the Blob constructor, which accepts two arguments:
- An array of data parts (strings, typed arrays like
Uint8Array, ArrayBuffers, or other Blobs) - An options object (optional) specifying properties like
type(the MIME type)
// From a string
const textBlob = new Blob(['Hello, world!'], { type: 'text/plain' });
// From multiple strings
const multiPartBlob = new Blob(['Hello', ' ', 'world'], { type: 'text/plain' });
// From a Uint8Array
const bytes = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
const binaryBlob = new Blob([bytes], { type: 'application/octet-stream' });
// Combining different types
const mixedBlob = new Blob(['Text: ', bytes], { type: 'text/plain' });
// From another Blob
const copiedBlob = new Blob([textBlob], { type: 'text/plain' });
The type option sets the MIME type, which tells systems how to interpret the data (like 'image/png' or 'application/json').
Key Takeaway: Use new Blob([parts], options) to create immutable binary data containers from strings, typed arrays, or other Blobs, optionally specifying a MIME type for proper data interpretation.