Search code examples
javascripttypesblob

How to check if a variable is a blob in JavaScript


As typeof returns "object"..

var MyBlob = new Blob(['test text'], {type : 'text/plain'});
console.log(typeof MyBlob) // "object"

is it too early to ask for a generic solution for checking whether or not a variable is a blob as it is not yet widely supported? Or how should I go about testing for blob type in browsers which already have it implemented?


Solution

  • You can test if it is an instanceof Blob like this:

    var MyBlob = new Blob(['test text'], {type : 'text/plain'});
    document.body.innerHTML = MyBlob instanceof Blob;

    This will work for things that inherit from Blob also.