Check if variable holds File or Blob

alexandernst picture alexandernst · Jul 20, 2015 · Viewed 35.4k times · Source

Javascript has both File and Blob for file representation, and both are almost the same thing. Is there a way to check if a variable is holding a File or a Blob type of data?

Answer

Nick Brunt picture Nick Brunt · Nov 29, 2017

Easiest way:

a = new File([1,2,3], "file.txt");
b = new Blob([1,2,3]);
c = "something else entirely";

a instanceof File
> true
b instanceof File
> false
c instanceof File
> false