Inheritance in JavaScript with example

JavaScript is an object-based language based on prototypes. Inheritance is implemented in JavaScript using the prototype object.

Following is the code to implement Inheritance in JavaScript −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   .result {
      font-size: 18;
      color: blueviolet;
      font-weight: 500;
<h1>JavaScript Inheritance</h1>
<div class="result"></div>
<button class="Btn">CLICK HERE</button>
Click on the above button to call the welcome method inherited by person1 and person2 object
   let BtnEle = document.querySelector(".Btn");
   let resEle = document.querySelector(".result");
   function Person(name, age, city) { = name;
      this.age = age; = city;
   Person.prototype.welcome = function () {
      resEle.innerHTML +=
      " Welcome "" age: "+this.age +" city: ""<br>";
   BtnEle.addEventListener("click", () => {
      let person1 = new Person("Rohan", 22, "Delhi");
      let person2 = new Person("Shawn", 19, "England");


On clicking the ‘CLICK HERE’ button −