+ 14

Unexpected Output In Javascript!

Check given code in where i created an object with user and pass and then i created a new variable with name anita and give the value of sneha but when i changed the value of anita.user it also change the value of sneha.user also how this is happening? https://code.sololearn.com/WILhQyRVVYrp/?ref=app

30th Sep 2020, 2:07 PM
Mask Man(What's in a name?)
Mask Man(What's in a name?) - avatar
4 ответов
+ 8
Naqab Posh ofcourse they both change) When you write sneha = anita you make them both point to the same object! So they both represent one object, which has two names (sneha and anita). So now you can use any of these two names to change this object’s attributes.
30th Sep 2020, 2:20 PM
Mikhail Solovyev
Mikhail Solovyev - avatar
+ 8
You have created only a single object with 2 references sneha and anita. It simply means that both reference are pointing to the same object and both can modify the object. Changes made by one will be reflected on the other as well. Congratulations you have learnt something new today👍
30th Sep 2020, 2:22 PM
Avinesh
Avinesh - avatar
+ 5
Avinesh thanks i learned 3 new things on searching this question's answer First :- primitive and non primitive data types Second:- "use strict" Third:- variable and function Hoisting
30th Sep 2020, 2:25 PM
Mask Man(What's in a name?)
Mask Man(What's in a name?) - avatar
+ 2
I searched about it and found best answer about it Object is of non primitive datatypes and also known as call by reference . That means it will Change the value on Change in any variable; In Above example I created a variable sneha with an object then object store on somewhere else and sneha has only an id i assigned value of sneha to anita then anita also have same id so if someone Change one then other will also change.🙏
30th Sep 2020, 2:12 PM
Mask Man(What's in a name?)
Mask Man(What's in a name?) - avatar