How to check if a variable is a blob in JavaScript

Jaak Kütt picture Jaak Kütt · Dec 22, 2013 · Viewed 28.9k times · Source

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?

Answer

jfriend00 picture jfriend00 · Dec 22, 2013

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

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

jsFiddle: http://jsfiddle.net/jfriend00/5xkgd/

This will work for things that inherit from Blob also.