Skip to content

Commit ab32639

Browse files
authored
Merge pull request #336 from akshith30388/add-question-477
Add question 477: structuredClone deep copy explanation
2 parents 17533ae + dfaa92b commit ab32639

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,8 @@
505505
| 474 | [What is module scope in JavaScript?](#what-is-module-scope-in-javascript) |
506506
| 475 | [What are shadowing and illegal shadowing?](#what-are-shadowing-and-illegal-shadowing) |
507507
| 476 | [Why is it important to remove event listeners after use?](#why-is-it-important-to-remove-event-listeners-after-use) |
508+
| 477 | [What is structuredClone and how is it used for deep copying objects?](#what-is-structuredclone-and-how-is-it-used-for-deep-copying-objects) |
509+
508510
<!-- TOC_END -->
509511

510512
<!-- QUESTIONS_START -->
@@ -9524,9 +9526,34 @@ Common use cases and benefits:
95249526
```
95259527
95269528
**[⬆ Back to Top](#table-of-contents)**
9529+
477. ### What is structuredClone and how is it used for deep copying objects?
95279530
9528-
<!-- QUESTIONS_END -->
9531+
In JavaScript, `structuredClone()` is a built-in method used to create a **deep copy** of a value. It safely clones nested objects, arrays, Maps, Sets, Dates, TypedArrays, and even circular references — without sharing references to the original value. This prevents accidental mutations and makes it useful for state management and data processing.
9532+
9533+
For example, the below snippet demonstrates deep cloning of a nested object,
95299534
9535+
```javascript
9536+
const originalObject = {
9537+
name: "Deep Copy Test",
9538+
nested: {
9539+
value: 10,
9540+
list: [1, 2, 3]
9541+
},
9542+
};
9543+
9544+
const deepCopy = structuredClone(originalObject);
9545+
9546+
// Modify cloned value
9547+
deepCopy.nested.value = 99;
9548+
deepCopy.nested.list.push(4);
9549+
console.log(originalObject.nested.value); // 10
9550+
console.log(deepCopy.nested.value); // 99
9551+
console.log(originalObject.nested.list); // [1, 2, 3]
9552+
console.log(deepCopy.nested.list); // [1, 2, 3, 4]
9553+
9554+
**[⬆ Back to Top](#table-of-contents)**
9555+
9556+
<!-- QUESTIONS_END -->
95309557
### Coding Exercise
95319558

95329559
#### 1. What is the output of below code

0 commit comments

Comments
 (0)