Blob Properties and Size
What you'll learn: How to inspect key Blob metadata—size and type—without reading or processing the entire binary contents.
Why Properties Matter
When working with Blobs (chunks of binary data), you often need to know about the data before deciding how to handle it. Think of it like checking a package's weight and label before opening it—you can make smarter decisions without unpacking everything first.
The Two Essential Properties
Every Blob instance exposes two read-only properties:
size
Returns the exact number of bytes in the Blob as an integer. This is crucial for validation, progress tracking, or deciding whether to process the data at all.
const myBlob = new Blob(['Hello, world!'], { type: 'text/plain' });
console.log(myBlob.size); // 13 (bytes)
const largeBlob = new Blob([new Uint8Array(1024 * 1024)]); // 1MB
console.log(largeBlob.size); // 1048576
type
Returns the MIME type string you specified during creation, or an empty string if none was provided. This tells you what kind of data the Blob contains.
const textBlob = new Blob(['data'], { type: 'text/plain' });
console.log(textBlob.type); // "text/plain"
const imageBlob = new Blob([new Uint8Array([255, 0, 0])], { type: 'image/png' });
console.log(imageBlob.type); // "image/png"
const unknownBlob = new Blob(['mystery']);
console.log(unknownBlob.type); // "" (empty string)
Practical Example
function shouldProcess(blob) {
if (blob.size > 5 * 1024 * 1024) { // Over 5MB?
console.log('Blob too large!');
return false;
}
if (!blob.type.startsWith('image/')) {
console.log('Not an image type');
return false;
}
return true;
}
Key Takeaway: Use blob.size to check byte count and blob.type to inspect MIME type—both operations are instant and don't require reading the Blob's contents.