Understanding the Prototype Chain in JavaScript Last Updated : 18 Jan, 2025 Comments Improve Suggest changes Like Article 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. Comment More infoAdvertise with us Next Article Understanding the Prototype Chain in JavaScript A abhishek18bme1037 Follow Improve Article Tags : JavaScript Web Technologies javascript-oop JavaScript-Questions Similar Reads What is the usage of Function.prototype.bind in JavaScript ? Bind method: Using this method, we can bind an object to a common function, so that gives different result when its needed. The bind() method takes an object as an argument and creates a new function. So basically bind function return function. Let's understand when bind method is used. bind the obj 2 min read Prototypal Inheritance using __proto__ in JavaScript Every object with its methods and properties contains an internal and hidden property known as [[Prototype]]. The Prototypal Inheritance is a feature in javascript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another objec 2 min read Prototype Inheritance in JavaScript Prototype inheritance in JavaScript allows objects to inherit properties and methods from other objects. Each object in JavaScript has an internal link to another object called its prototype. This chain of prototypes forms the prototype chain.When you access a property or method on an object, JavaSc 3 min read JavaScript String prototype Property The prototype property allows to add new properties and methods to the existing JavaScript object types. There are two examples to describe the JavaScript String prototype property. Syntax: object.prototype.name = valueReturn Value: It returns a reference to the String.prototype object.Example 1: Th 2 min read Method Chaining in JavaScript As a good programming practice, we should write individual functions/methods for dealing with individual actions. And, writing only one method/function for all actions is a thing. However, sticking to good practice takes a toll on the readability and comprehensibility of the code, because defining a 2 min read How to create an object with prototype in JavaScript ? In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i 4 min read JavaScript Prototype In JavaScript, everything is an object, including functions, arrays, and strings, which are specialized types of objects. JavaScript follows a prototype-based system, where objects inherit properties and methods from other objects through prototypes. This prototype mechanism plays a key role in how 9 min read What is the Call Stack in JavaScript ? In JavaScript, the Call Stack is an essential concept that helps the JavaScript engine keep track of function execution. It plays a vital role in managing the execution order of functions and determining how the JavaScript program handles function calls.How Does the Call Stack Work?JavaScript operat 4 min read Creating objects in JavaScript An object in JavaScript is a collection of key-value pairs, where keys are strings (properties) and values can be any data type. Objects can be created using object literals, constructors, or classes. Properties are defined with key-value pairs, and methods are functions defined within the object, e 5 min read JavaScript Object.prototype.toString() Method In JavaScript, the Object.prototype.toString() method is used to return a string that can represent the object. The toString() method is automatically inherited by every object which is inherited from Object. Whenever an object is represented as a text value or a string is expected from the object, 3 min read Like