farinspace farinspace - 3 months ago 13x
Javascript Question

Which is best to use: typeof or instanceof?

In my particular case:

callback instanceof Function


typeof callback == "function"

does it even matter, what's the difference?

Additional Resource:

JavaScript-Garden typeof vs instanceof


Both are similar in functionality because they both return type information, however I personally prefer instanceof because it's comparing actual types rather than strings. Type comparison is less prone to human error, and it's technically faster since it's comparing pointers in memory rather than doing whole string comparisons.