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
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
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.
A good reason to use typeof is if the variable may be undefined.
alert(typeof undefinedVariable); // alerts the string "undefined"
alert(undefinedVariable instanceof Object); // throws an exception
A good reason to use instanceof is if the variable may be null.
var myNullVar = null;
alert(typeof myNullVar ); // alerts the string "object"
alert(myNullVar instanceof Object); // alerts "false"
So really in my opinion it would depend on what type of possible data you are checking.
Other Significant practical differences:
// Boolean
var str3 = true ;
alert(str3);
alert(str3 instanceof Boolean); // false: expect true
alert(typeof str3 == "boolean" ); // true
// Number
var str4 = 100 ;
alert(str4);
alert(str4 instanceof Number); // false: expect true
alert(typeof str4 == "number" ); // true
typeof: Per the MDN docmentation, typeof is a unary operator that returns a string indicating the type of the unevaluated operand.
In the case of string primitaves and string objects, typeof returns the following:
const a = "I'm a string primitive";
const b = new String("I'm a String Object");
typeof a; --> returns 'string'
typeof b; --> returns 'object'
instanceof: is a binary operator, accepting an object and a constructor. It returns a boolean indicating whether or not the object has the given constructor in its prototype chain.
When applied to the string instances above, and compared to String, it behaves as follows:
const a = "I'm a string primitive";
const b = new String("I'm a String Object");
a instanceof String; --> returns false
b instanceof String; --> returns true
Reference: https://bambielli.com/til/2017-06-18-typeof-vs-instanceof/
instanceof in Javascript can be flaky - I believe major frameworks try to avoid its use. Different windows is one of the ways in which it can break - I believe class hierarchies can confuse it as well.
There are better ways for testing whether an object is a certain built-in type (which is usually what you want). Create utility functions and use them:
function isFunction(obj) {
return typeof(obj) == "function";
}
function isArray(obj) {
return typeof(obj) == "object"
&& typeof(obj.length) == "number"
&& isFunction(obj.push);
}
And so on.
I would recommend using prototype's callback.isFunction().
They've figured out the difference and you can count on their reason.
I guess other JS frameworks have such things, too.
instanceOf wouldn't work on functions defined in other windows, I believe.
Their Function is different than your window.Function.
Use instanceof because if you change the name of the class you will get a compiler error.
no need to overwhelm with ton of above examples, just keep in mind two points of view:
typeof var; is an unary operator will return the original type or root type of var. so that it will return primitive types(string, number, bigint, boolean, undefined, and symbol) or object type.
in case of higher-level object, like built-in objects (String, Number, Boolean, Array..) or complex or custom objects, all of them is object root type, but instance type built base on them is vary(like OOP class inheritance concept), here a instanceof A - a binary operator - will help you, it will go through the prototype chain to check whether constructor of the right operand(A) appears or not.
so whenever you want to check "root type" or work with primitive variable - use "typeof", otherwise using "instanceof".
null is a special case,which is seemingly primitive, but indeed is a special case for object. Using a === null to check null instead.
on the other hand, function is also a special case, which is built-in object but typeof return function
as you can see instanceof must go through the prototype chain meanwhile typeof just check the root type one time so it's easy to understand why typeof is faster than instanceof
To be very precise instanceof should be used where value is created via the constructor (generally custom types) for e.g.
var d = new String("abc")
whereas typeof to check values created just by assignments for e.g
var d = "abc"
According to MDN documentation about typeof, objects instantiated with the "new" keyword are of type 'object':
typeof 'bla' === 'string';
// The following are confusing, dangerous, and wasteful. Avoid them.
typeof new Boolean(true) === 'object';
typeof new Number(1) === 'object';
typeof new String('abc') === 'object';
While documentation about instanceof points that:
const objectString = new String('String created with constructor');
objectString instanceOf String; // returns true
objectString instanceOf Object; // returns true
So if one wants to check e.g. that something is a string no matter how it was created, safest approach would be to use instanceof.
Coming from a strict OO upbringing I'd go for
callback instanceof Function
Strings are prone to either my awful spelling or other typos. Plus I feel it reads better.