---
title: "Explain the concept of prototypal inheritance in JavaScript."  
description: "Explain the concept of prototypal inheritance in JavaScript."  
author: "Revati S Misra"  
published: 2024-06-26  
updated: 2024-06-26  
canonical: https://www.mindstick.com/forum/160786/explain-the-concept-of-prototypal-inheritance-in-javascript  
category: "javascript"  
tags: ["javascript", "javascript object", "inheritance"]  
reading_time: 3 minutes  

---

# Explain the concept of prototypal inheritance in JavaScript.

[Explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of prototypal inheritance in JavaScript.

## Replies

### Reply by Ashutosh Patel

#### JavaScript prototypal inheritance

Prototype inheritance is a key [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) in JavaScript where objects inherit properties and methods from other objects rather than from classes as in traditional class-based inheritance models this is achieved through instances.

#### Key Concepts

## Prototypes

- Every object in JavaScript has a prototype. Prototypes are basically fallback objects that the object can inherit and a way to find someone’s hand if they are not found on the object itself.
- When you access a property or method on an object, JavaScript checks that object directly first. If not found, it then searches for an instance of the object, and continues up the initial chain until a property or method is found or until it reaches the end of the chain (where the original chain is `null`).

## Prototype Chain

- A preliminary chain is created by setting one object as a prototype for another object. This provides communication between objects, allowing access to properties and properties of methods.
- JavaScript objects can be linked together through their instances, forming a chain that JavaScript uses to resolve inheritance access.

## Constructor Functions

- Constructor functions in JavaScript are used to create objects with specific attributes and methods. It is often used to start objects that will share the same primitive chain.
- When a constructor creates an instance of a new object with the `new` keyword, the newly created object inherits **properties** and **methods** from the prototype of the constructor function.

`prototype` **Property**

- Every function in JavaScript has a `prototype` property, where properties and methods can be added sequentially by instantiating them as constructors to that function.
- When an object is created using a constructor function, its prototype is set to the constructor's `prototype` property.

**Also, Read:** [Prototypal Inheritance vs. Class-Based Inheritance in JavaScript](https://www.mindstick.com/articles/336238/prototypal-inheritance-vs-class-based-inheritance-in-javascript)

## Example-

```javascript
// Constructor function
function Person(name, age) {
 this.name = name;
 this.age = age;
}
// Adding methods to the prototype of Person
Person.prototype.sayHello = function() {
 console.log(`Hello, my name is ${this.name}`);
};
// Creating instances of Person
let person1 = new Person('Alice', 30);
let person2 = new Person('Bob', 25);
// Accessing properties and methods
console.log(person1.name); // Output: "Alice"
person2.sayHello(); // Output: "Hello, my name is Bob"
```

## In the example above-

`Person` is a constructor function that creates instances with `name` and `age` properties.

The `sayHello` method has been added to `Person.prototype`, making it available to all instances created by `Person`.

`person1` and `person2` inherit properties (`name`, `age`) and methods (`sayHello`) from `Person.prototype`.

**Also, Read:** [What is event delegation in JavaScript, and why is it useful?](https://www.mindstick.com/forum/160785/what-is-event-delegation-in-javascript-and-why-is-it-useful)


---

Original Source: https://www.mindstick.com/forum/160786/explain-the-concept-of-prototypal-inheritance-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
