farinspace farinspace - 4 months ago 19
Javascript Question

Which is best to use: typeof or instanceof?

In my particular case:

callback instanceof Function


or

typeof callback == "function"


does it even matter, what's the difference?

Additional Resource:

JavaScript-Garden typeof vs instanceof

Answer

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.