You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+6-7Lines changed: 6 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -458,7 +458,7 @@ const printName = () => {
458
458
459
459
**[:top: Scroll to Top](#javascript-output-based-interview-questions)**
460
460
461
-
**1. What will be the output (shallow copy of an object)**
461
+
**28. What will be the output (shallow copy of an object)**
462
462
```js
463
463
constuserDetails= {
464
464
firstName:"Surbhi",
@@ -471,21 +471,20 @@ const userDetails = {
471
471
};
472
472
473
473
let cloneUserDetails = { ...userDetails };
474
-
475
474
//Updating original object
476
475
userDetails.age=22;
477
476
userDetails.address.city="Banglore";
478
477
479
-
console.log(cloneUserDetails.age); // what will be the output
480
-
console.log(cloneUserDetails.address.city);// what will be the output
478
+
console.log(cloneUserDetails.age);
479
+
console.log(cloneUserDetails.address.city);
481
480
```
482
481
<details>
483
482
<summary><b>View Answer</b></summary>
484
483
<ul>
485
484
<li><b>Output</b> : 20, "Banglore"</li>
486
-
<li><b>Explanation </b> : cloneUserDetails is created by using the spread syntax ({ ...userDetails }). This syntax creates a shallow copy of the userDetails object, meaning that the top-level properties are copied, but nested objects are still referenced.</li>
487
-
<li><b>case 1</b> : Although userDetails.age was changed to 22, cloneUserDetails still holds the original value of 20. This is because the spread syntax only creates a shallow copy, so the age property of cloneUserDetails remains unchanged.</li>
488
-
<li><b>case 2</b> : The nested address object is still referenced by cloneUserDetails, so when the city property of userDetails.address is changed, it reflects in cloneUserDetails.address as well. Therefore, the output is "Banglore".</li>
485
+
<li><b>Reason </b> : cloneUserDetails is created by using the spread syntax ({ ...userDetails }). This syntax creates a shallow copy of the userDetails object, meaning that the top-level properties are copied, but nested objects are still referenced.</li>
486
+
<li><b>case 1</b> : Although userDetails.age was changed to 22, cloneUserDetails still holds the original value of 20. This is because the spread syntax only creates a shallow copy, so the age property of cloneUserDetails remains unchanged.</li>
487
+
<li><b>case 2</b> : The nested address object is still referenced by cloneUserDetails, so when the city property of userDetails.address is changed, it reflects in cloneUserDetails.address as well. Therefore, the output is "Banglore".</li>
0 commit comments