How to Extend an Object in JavaScript ? Last Updated : 25 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Extending an object in JavaScript means adding properties or methods to enhance its functionality. This can be done dynamically. The extends keyword is used to create a subclass from a parent class, enhancing object-oriented programming flexibility in JavaScript. Syntax:class childclass extends parentclass {...} class parentclass extends in-built object {...} The following example shows how a child class inherits properties from a parent class using the extends keyword. The Profile class has two attributes, name and age. The `Student` class extends Profile, adds attribute languages, and displays all attributes. Example: In this example, we use the extends keyword. JavaScript // Declaring class class Profile { // Constructor of profile class constructor(name, age) { this.name = name; this.age = age; } getName() { // Method to return name return this.name; } getAge() { // Method to return age return this.age; } getClass() { return this; } } // Class Student extends class Profile class Student extends Profile { // Each data of class Profile can be // accessed from student class. constructor(name, age, languages) { // Acquiring of attributes of parent class super(name, age); this.lang = [...languages]; } // Method to display all attributes getDetails() { console.log("Name : " + this.name); console.log("Age : " + this.age); console.log("Languages : " + this.lang); } } // Creating object of child class with passing of values let student1 = new Student("Ankit Dholakia", 24, ['Java', 'Python', 'PHP', 'JavaScript']); student1.getDetails(); OutputName : Ankit Dholakia Age : 24 Languages : Java,Python,PHP,JavaScript Example: In this example, we will see how spread syntax can be used to extend two objects into a third object and display the containing attributes of both objects. JavaScript // Creating first object let obj1 = { name: 'Ankit', age: 20 }; // Creating second object let obj2 = { marks: 50 }; // Using spread syntax to extend // both objects into one let object = { ...obj1, ...obj2 }; console.log(object); Output{ name: 'Ankit', age: 20, marks: 50 } Comment More infoAdvertise with us Next Article How to Extend an Object in JavaScript ? A ankit0812 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Misc JavaScript-Questions Similar Reads How to Deep-Freeze an Object in JavaScript? In JavaScript, freezing an object prevents it from being modified. This means you can no longer add, remove, or modify the properties of an object. However, by default, JavaScript's Object.freeze() only freezes the top level of an object, leaving nested objects or arrays mutable. To achieve immutabi 4 min read How to Create a Nested Object in JavaScript ? JavaScript allows us to create objects having the properties of the other objects this process is called as nesting of objects. Nesting helps in handling complex data in a much more structured and organized manner by creating a hierarchical structure. These are the different methods to create nested 4 min read How to Clone a JavaScript Object? Here are different methods to clone a JavaScript object.1. Using Object.assign() MethodThe Object.assign() method is used to clone the given object. We have assigned the given object to the new empty object by just using this method. Syntaxlet Clone_object =Object.assign({}, sourceObject);Note: This 2 min read How to Create Dynamic Values and Objects in JavaScript? In JavaScript, you can choose dynamic values or variable names and object names and choose to edit the variable name in the future without accessing the array, Dynamic values and objects in JavaScript allow changing, accessing, or creating values and object properties at runtime for flexibility and 3 min read How to print the content of an object in JavaScript ? To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: JavaScript // Given Object const obj = { name: 'John', age: 30, city: 3 min read How to create object properties in JavaScript ? JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added, 4 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 How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one 2 min read How to Deep clone in JavaScript? Deep clone in JavaScript refers to creating a complete copy of an object, including all nested objects, ensuring that changes to the cloned object do not affect the original. Unlike shallow cloning, deep cloning requires copying all levels of the objectâs structure.1. Using Spread Operator to Deep C 2 min read How to iterate over a JavaScript object ? Iteration involves looping through the object's properties one by one. Depending on the method used, you can access and manipulate different levels of properties efficiently. Here are several methods.There are many methods to iterate over an object which are discussed below: Table of ContentUsing fo 3 min read Like