What is the prototype chain in JavaScript?
Answer
The prototype chain is the mechanism JavaScript uses for property lookup and inheritance. When accessing a property on an object, JavaScript first checks the object's own properties. If not found, it checks [[Prototype]] (the prototype), then the prototype's prototype, and so on until reaching null. Example: const arr = [1, 2, 3] — arr.push is not on the array itself but found on Array.prototype. arr.toString is found further up on Object.prototype. All objects ultimately trace back to Object.prototype which itself has null as its prototype. Object.create(proto) creates an object with a specific prototype. Object.getPrototypeOf(obj) returns the prototype. The prototype chain enables: method sharing (all array instances share Array.prototype.push), inheritance hierarchies, and mixins. Extending built-in prototypes is considered bad practice (monkey-patching).
Previous
What is the difference between indexOf and includes?
Next
What is ES6 class syntax in JavaScript?