What's the difference between instanceof, isPrototypeOf and constructor ===

Tell us what’s happening:
In the last few challenges, I have learned about the instanceof, constructor === and now, isPrototypeOf. They have seems the same to me: when a constructor function creates a new object, it inherits its prototype and also is an instance of its constructor. The console log below from my code all output to true. So what’s the difference in using those 3?

function Dog(name) {
this.name = name;

let beagle = new Dog("Snoopy");

// Only change code below this line

console.log(Dog.prototype.isPrototypeOf(beagle)); //true
console.log(beagle.constructor === Dog); //true
console.log(beagle instanceof Dog); //true
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Understand Where an Object’s Prototype Comes From

Link to the challenge:

@DanCouper gave a great answer to this very question: Difference among instaceof,constructor property,isPrototypeOf

Another distinction, this one from the MDN:

Note: isPrototypeOf() differs from the instanceof operator. In the expression " object instanceof AFunction ", the object prototype chain is checked against AFunction.prototype , not against AFunction itself.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.