farinspace farinspace - 1 year ago 106
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

Answer Source

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.