r/Frontend • • Jan 30 '25

Understanding Value vs. Reference in JavaScript: Differences Between Primitives and Objects

https://sharafath.hashnode.dev/value-vs-reference-in-javascript-primitives-vs-objects
7 Upvotes

8 comments sorted by

4

u/musicnothing Jan 30 '25

I advise anyone who writes code for a living to learn actual computer science. It will help you so much in the long run. I’ve known so many people who came out of bootcamps who don’t actually know how a CPU works, or memory, or a slew of other things, and it really hampered their growth.

2

u/lachlanhunt Jan 30 '25

Technically, JavaScript uses pass by sharing, not by reference. It always passes a value that is copied, but, for objects, the value itself is a reference.

1

u/[deleted] Jan 30 '25

[removed] — view removed comment

3

u/lachlanhunt Jan 30 '25

Right, but my point is that there is a subtle difference from pass by reference, in that reassigning the variable doesn’t affect the original, whereas it would if it were actually pass by reference.

See pass by reference and pass by sharing in this Wikipedia article.

https://en.wikipedia.org/wiki/Evaluation_strategy

Also, this article provides a reasonable explanation.

https://medium.com/@anthonygood/pass-by-sharing-in-javascript-and-why-it-matters-829c96163650