instanceof operator in JavaScript

The instanceof operator is used to check the type of an object at run time. Every object in JavaScript has a prototype, accessible through the __proto__ property . Functions also have a prototype property, which is the initial __proto__ for any objects created by them. When a function is created, it is given a unique object for prototype. The instanceof uses the prototype of an object to determine if it's an instance of a class or a constructed function. It returns a boolean value that indicates if an object is an instance of a particular class.
const Animal = function(type) { this.type = type; } const dog = new Animal('dog'); document.write(dog instanceof Animal); //returns true document.write(dog instanceof Object); //returns true

Which is best to use: instanceof or typeof?

Both are similar in functionality because they both return type information, however, it is better to 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.