Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0e0090d

Browse files
committed
new question added
1 parent 965b46e commit 0e0090d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,32 @@ So the object `a` looks like -
5959
```
6060

6161
</details>
62+
63+
<details>
64+
<summary>
65+
<h3>2. Guess the output of this code?
66+
67+
```js
68+
let obj1 = { key: "value" };
69+
let obj2 = obj1;
70+
let obj3 = obj2;
71+
72+
obj1.key = "new value";
73+
obj2 = { key: "another value" };
74+
75+
console.log(obj1.key, obj2.key, obj3.key);
76+
```
77+
78+
</h3>
79+
</summary>
80+
The output of this code will be `new value` `another value` `new value`.
81+
82+
In this code, we are declaring three variables obj1, obj2, and obj3, and assigning an object to each of them. Then, we are reassigning a new object to obj2 and modifying a property of obj1.
83+
84+
When the console.log statement is executed, it logs the values of the key property for each object. The value of the key property for obj1 is "new value", the value of the key property for obj2 is "another value", and the value of the key property for obj3 is "new value".
85+
86+
This is because when an object is assigned to a variable, the variable stores a reference to the object in memory rather than the object itself. Changing the value of a property of the object using one variable will affect the value of that property when accessed using a different variable that references the same object. However, reassigning a new object to a variable will change the reference stored in that variable, so the original object is no longer accessible using that variable.
87+
88+
In this case, the value of the key property for obj1 was changed to "new value" using the obj1 variable, which affected the value of the key property when accessed using the obj3 variable, because both variables reference the same object. However, the value of the key property for obj2 was not affected, because the obj2 variable was reassigned to reference a new object.
89+
90+
</details>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /