Understanding the Prototype Chain in JavaScript Last Updated : 18 Jan, 2025 Comments Improve Suggest changes 5 Likes Like Report The prototype chain is a core JavaScript concept enabling the inheritance of properties and methods between objects. It facilitates code reuse, efficient property lookup, and object hierarchy creation.Every JavaScript object has an internal link to another object, called its prototype.The prototype chain forms when objects inherit properties and methods from their prototypes.Property or method access starts from the object itself and traverses up the chain if not found.The chain ends at null, the prototype of Object.prototype. JavaScript const parent = { greet: () => "Hello!" }; const child = Object.create(parent); //Driver Code Starts console.log(child.greet()); console.log(Object.getPrototypeOf(child) === parent); //Driver Code Ends In this exampleparent is the prototype of child.child inherits the greet method from parent.The chain consists of child → parent → Object.prototype → null.Syntaxfunction ConstructorName(params) { // Initialization code}ConstructorName.prototype.methodName = function () { // Method code};Real-World Use CasesExtending Built-in Objects JavaScript Array.prototype.sum = function () { return this.reduce((acc, val) => acc + val, 0); }; console.log([1, 2, 3].sum()); Output6 Custom Object Hierarchies JavaScript const vehicle = { start() { console.log("Engine started."); }, }; const car = Object.create(vehicle); car.drive = function () { console.log("Car is driving."); }; car.start(); car.drive(); OutputEngine started. Car is driving. Checking Property Existence JavaScript const car = { wheels: 4 }; const myCar = Object.create(car); myCar.color = "red"; //Driver Code Starts console.log("color" in myCar); console.log("wheels" in myCar); console.log(myCar.hasOwnProperty("wheels")); //Driver Code Ends Outputtrue true false Prototype Chain Traversal JavaScript function fun(obj) { let current = obj; while (current) { console.log(current); current = Object.getPrototypeOf(current); } } const animal = { eats: true }; const mammal = Object.create(animal); mammal.hasFur = true; const dog = Object.create(mammal); dog.barks = true; fun(dog); Output{ barks: true } { hasFur: true } { eats: true } [Object: null prototype] {} Prototype Method Overriding JavaScript function Vehicle() { } Vehicle.prototype.start = function () { return "Vehicle is starting"; }; function Car() { } Car.prototype = Object.create(Vehicle.prototype); Car.prototype.constructor = Car; Car.prototype.start = function () { return "Car is starting"; }; const myCar = new Car(); console.log(myCar.start()); OutputCar is starting Advantages of the Prototype ChainCode Reusability: Shared properties and methods reduce redundancy.Efficient Memory Usage: Shared prototypes lower memory consumption.Dynamic Behavior: Prototypes can be extended at runtime.Scalable Object Hierarchies: Simplifies creation and management of relationships between objects. Create Quiz Comment A abhishek18bme1037 Follow 5 Improve A abhishek18bme1037 Follow 5 Improve Article Tags : JavaScript Web Technologies javascript-oop JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like