JavaScript instanceof operator
The instanceof operator is used to compare the constructors of the two operands. It is only meaningful to compare a custom object.
If used to compare built-in types, it will be as useful as the TypeOf operator.
Comparing Custom Objects
function Foo () {}
function Bar () {}
Bar.prototype = new Foo ();
New Bar () instanceof Bar; True
New Bar () instanceof Foo; True
If you only set Bar.prototype as a function of Foo, not an instance of the Foo constructor
Bar.prototype = Foo;
New Bar () instanceof Foo; False
instanceof Compare built-in types
New string (' foo ') instanceof string; True
New String (' foo ') instanceof Object; True
' foo ' instanceof String; False
' foo ' instanceof Object; False
It is important to note that instanceof is used to compare objects belonging to different JavaScript contexts (for example, different document structures in browsers),
Because their constructors are not the same object.
Conclusion
The instanceof operator should be used only to compare custom objects from the same JavaScript context.
As with the typeof operator, any other usage should be avoided.