AceDevHub
Intermediate JavaScript Interview QuestionsIntermediateConcept

JavaScript · Question 39

How does the prototype chain work in JavaScript?

Direct answer

When a property is not found as an own property, JavaScript follows the object’s internal [[Prototype]] link and repeats the lookup until it finds the property or reaches null.

JavaScript inheritance is based on delegation through prototypes. If obj.x is requested and x is not an own property of obj, lookup continues on Object.getPrototypeOf(obj) and then on that object’s prototype, continuing until null.

Methods such as array methods usually illustrate this well: an array instance does not contain its own copy of map. The method is found through Array.prototype in its prototype chain.

  • Object.hasOwn(obj, key) checks whether the property is owned directly by the object.
  • key in obj checks both own and inherited properties.
  • Assigning an own property with the same name can shadow an inherited property without modifying the prototype.