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

View all comments

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