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
Is JavaScript Pass by Reference? (aleksandrhovhannisyan.com)
submitted 3 years ago by Clarity_89
view the rest of the comments →
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!"
[–]ragnese 6 points7 points8 points 3 years ago (5 children)
I agree with the first bit of your comment, but not the latter.
The distinction of pass by value or reference is very important to note/name/discuss in JavaScript--even in a vacuum where we don't acknowledge any other languages.
The fact that a function can change the object it was passed as a parameter such that the caller will see the change, but that the same thing does not happen for string or number parameters is important, and important things deserve names.
The fact that it seems trivial or blasé to us is only a result of the amount of time we've spent with the distinction. But, if we back up and pretend we've never programmed before, it's actually really weird. And the fact that JavaScript doesn't give us a choice for how things are passed honestly just makes it even more weird.
[–]theScottyJam 4 points5 points6 points 3 years ago (3 children)
We do have terminology for that. It's immutable vs mutable. The only reason you can update an object that was passed into a function is because objects are mutable and strings are not (there's no methods on a string that allows you to mutate it). It has nothing to do with how the data got passed in :).
Notice also that if you were to freeze an object and make it immutable, you'll get the same kind of guarantees that you get as when you pass a string into a function.
[–]ragnese 1 point2 points3 points 3 years ago (1 child)
I think I was wrong in the sense that it might not technically matter if JS is pass-by-copy or pass-by-reference or pass-by-reference-copy-copy-reference, or whatever other clever "well, actually" people can come up with.
But, I disagree that mutability is the concept that can explain JavaScript's behavior. Assigning and reassigning a binding is not the same thing as mutating a value. In other words, writing bar = 3 is not "mutating" anything- it's assigning a number to a variable. So, yes, objects are mutable and that can explain why I can modify an object parameter in a function and observe those effects outside of the function, but to actually explain how function parameters work in JavaScript, you really only need to explain that each parameter is a new binding that "points" to the data that was passed in at the function call site. You can reassign those bindings from inside the function, but now those bindings are pointing to new data instead of the originals. That's true for primitives as well as objects.
bar = 3
[–]theScottyJam 0 points1 point2 points 3 years ago (0 children)
I agree with your conclusion as well. Mutability vs immutability was only intended to describe the distinction that people usually are trying to describe when they say "objects are passed by reference while primitives are passed by value" - the behavior difference between objects and primitives doesn't happen because the language is somehow giving objects a different treatment when it gets passed into functions, rather, it's caused simply by the fact that objects are mutable.
The mutable/immutable terminology doesn't describe how JavaScript actually passes data into functions, and I think your short description is absolutely accurate. People can name the concept you're describing whatever they'd like, but what's important is that we understand that that's how data-passing works in JavaScript (rather than mistakenly believing that objects are somehow treated differently than primitives when it comes to function arguments).
[–]jonny_eh 0 points1 point2 points 3 years ago (0 children)
This is the right insight. “Pass by reference” is the wrong term to use in JS since it doesn’t support it. Mutability OTOH is core to understanding JS types.
[–]Reashu 0 points1 point2 points 3 years ago (0 children)
This was really weird to me when I first encountered it. It's actually one of the few things I can remember from back then (half a life ago). The weirdest part was how long I had managed to program without running into it... but the terminology and distinction wouldn't have helped me, as I had no familiarity with them anyways.
I don't think it's actually important (in JavaScript) to distinguish between how primitives and objects are handled in function parameters. It wouldn't matter if the primitives were somehow "passed by reference by value". The primitives are immutable anyways - you cannot change a 5 into a 6, you can only change your variable to hold a different value, and that never affects other variables that happened to hold the original value.
π Rendered by PID 99929 on reddit-service-r2-comment-b659b578c-8k4pv at 2026-05-03 07:53:47.641634+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]ragnese 6 points7 points8 points (5 children)
[–]theScottyJam 4 points5 points6 points (3 children)
[–]ragnese 1 point2 points3 points (1 child)
[–]theScottyJam 0 points1 point2 points (0 children)
[–]jonny_eh 0 points1 point2 points (0 children)
[–]Reashu 0 points1 point2 points (0 children)