instanceof operator lets you check if an object is an instance of a particular type of object (no shit, Sherlock).
More specifically, the MDN Web Docs state:
instanceofoperator tests to see if the
prototypeproperty of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.
Let’s look at a simple example.
In the code above:
- We have a constructor function that lets us create Pokemon objects.
- We create a Pokemon object and save it to the
pikachu instanceof Pokemonshows
truein the console.
The console shows
true because the
pikachu variable holds an object that is an instance of the
In my next post, I’ll show you a recent use case I found for the
instanceof operator. In the meantime, you can learn more on the MDN Web Docs.
If you have questions, feedback, or any other suggestions, please do email me. I'd love to hear from you!