How to Clone a JavaScript Object? Last Updated : 17 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 method does only a shallow copy. It means that nested properties are still copied by reference. JavaScript const sourceObject = { a: 1, b: 2, c: 3 }; let Clone_object = {}; Clone_object = Object.assign({}, sourceObject); console.log(Clone_object); Output{ a: 1, b: 2, c: 3 } 2. Using Spread Operator This approaches uses Spread operator for the cloning of object. It assigned all the elements to the new object. JavaScript const sourceObject = { a: 1, b: 2, c: 3 }; let Clone_object = {}; Clone_object = {...sourceObject}; console.log(Clone_object); Output{ a: 1, b: 2, c: 3 } 3. Using hasOwnProperty() MethodThis approaches uses hasOwnProperty() Method. we have used a loop to iterate through the source object and assigning them to new object by using the hasOwnProperty() method. JavaScript const sourceObject = {a:1, b:2, c:3}; let Cloned_Object = {}; for (let prop in sourceObject) { if (sourceObject.hasOwnProperty(prop)) { Cloned_Object[prop] = sourceObject[prop]; } } console.log(Cloned_Object); Output{ a: 1, b: 2, c: 3 } 4. Using JSON.stringify() MethodThis approach uses JSON.stringify() Method to clone a JavaScript Object. We are parsing the JSON.stringify() object to JSON.parse() so that it can be cloned to new empty object. JavaScript const sourceObject = { a: 1, b: 2, c: 3 }; let Cloned_object = {}; Cloned_object = JSON.parse(JSON.stringify(sourceObject)); console.log(Cloned_object); Output{ a: 1, b: 2, c: 3 } Comment More infoAdvertise with us Next Article How to Clone a JavaScript Object? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-object 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 clone an array in JavaScript ? In JavaScript, cloning an array means creating a new array with the same elements as the original array without modifying the original array.Here are some common use cases for cloning an array:Table of ContentUsing the Array.slice() MethodUsing the spread OperatorUsing the Array.from() MethodUsing t 6 min read JavaScript - Convert an Array to an Object Here are some common methods which can be used to convert an array to an object: 1. Using JavaScript Object.assign() method The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.JavaScrip 3 min read JavaScript- Add an Object to JS Array In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively. These are the following ways to add an object to a JS array: Using JavaScript Array 4 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 Like