How do you create a deep copy of an object in JavaScript?

How do you create a deep copy of an object in JavaScript?

a) JSON.parse(JSON.stringify(object))
b) Object.assign()
c) object.slice()
d) Array.map()

Answer:

a) JSON.parse(JSON.stringify(object))

Explanation:

To create a deep copy of an object in JavaScript, you can use JSON.parse(JSON.stringify(object)). This method works by first converting the object into a JSON string and then parsing it back into a new object. This creates a new, independent copy of the original object.

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top