Categories
JavaScript Answers

How to Check if a Variable is a Blob in JavaScript?

Spread the love

To check if a variable is a blob in JavaScript, we can use the instanceof operator.

For instance, we can write:

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

to create a blob with:

const myBlob = new Blob(['test text'], {
  type: 'text/plain'
});

We use the Blob constructor with the content for the blob as the first argument.

Then 2nd argument is an object with the type property to set the MIME type of the blob.

Then we use:

console.log(myBlob instanceof Blob)

to use the instanceof operator check if myBlob is a Blob instance.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

One reply on “How to Check if a Variable is a Blob in JavaScript?”

Leave a Reply

Your email address will not be published. Required fields are marked *