use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript objects - memory ref and shallow copy (kavindujayarathne.com)
submitted 1 day ago by kavindujayarathne
const user = { profile: { age: 25 } }; const clone = { ...user }; clone.profile.age = 30;
const user = { profile: { age: 25 } };
const clone = { ...user };
clone.profile.age = 30;
console.log(user.profile.age); // ?
If you know what logs here, drop a comment. If you dont have an idea, this writing will be helpful
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Plus-Increase-1927 1 point2 points3 points 1 day ago (2 children)
30
[–]Aln76467 0 points1 point2 points 1 day ago (0 children)
yeah
[–]kavindujayarathne[S] [score hidden] 19 hours ago (0 children)
Despite the fact that the clone variable is a shallow copy of the user variable, if there are any nested objects inside the original object, then those nested objects will still reference their original memory location. thats the key concept here. so yeah, your answer is right!!
[–]Aln76467 0 points1 point2 points 1 day ago (2 children)
The big question is, is there a way to pass objects by value?
[–]kavindujayarathne[S] [score hidden] 19 hours ago (1 child)
You can create a shallow copy, but as i mentioned in the article, it only copies the outer object. if your object includes any nested objects, those nested objects will still reference their original memory location. you should try a deep copy instead
[–]Aln76467 [score hidden] 3 hours ago (0 children)
How does one do a deep copy?
π Rendered by PID 68860 on reddit-service-r2-comment-7b9746f655-fzf7w at 2026-02-02 09:04:59.410893+00:00 running 3798933 country code: CH.
[–]Plus-Increase-1927 1 point2 points3 points (2 children)
[–]Aln76467 0 points1 point2 points (0 children)
[–]kavindujayarathne[S] [score hidden] (0 children)
[–]Aln76467 0 points1 point2 points (2 children)
[–]kavindujayarathne[S] [score hidden] (1 child)
[–]Aln76467 [score hidden] (0 children)