28

I was looking for a way to check if an object is a FormData instance, similarly to Array.isArray()

km6
  • 2,191
  • 2
  • 15
  • 18

1 Answers1

57

Use instanceof

For example:

let formData = new FormData()
let time = new Date()

console.log("statement: formData is a FormData instance", formData instanceof FormData) // statement is true
console.log("statement: time is a FormData instance", time instanceof FormData) // statment is false

Source

km6
  • 2,191
  • 2
  • 15
  • 18
  • 3
    The reason there's an *isArray* mehtod is that *instanceof* does not work across frames (or global environments) where the constructor is in one environment but the object was created in the other. You seem to have dismissed *instanceof* in your OP, then post an answer using it. What's the point? – RobG Sep 11 '17 at 00:58
  • @RobG The main point is that instead of implying that my question was a duplicate of another one regarding the usage of `instanceof`, it should have been presented as an answer explaining how `instanceof` could solve my problem - which is exactly what I did in my own response. At the time of posting, I wasn't aware of `instanceof`, and that was the message I intended to convey in my original post. I didn't dismiss`instanceof`. – km6 May 15 '23 at 11:03