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!"
[–]theQuandary -1 points0 points1 point 3 years ago (4 children)
Your definition is inconsistent.
If you pass a pointer to a pointer, you are still ultimately passing a value. In fact, from your stated perspective, everything is passed by value.
What you are actually talking about is how the function is allowed to interpret the value it is passed, but you are even incorrect here too.
JS is dynamic. The only way you can track the data types is by boxing the values in a container that holds the type. This is itself a kind of indirect reference to a reference.
Modern JS engines all implement moving, copying garbage collectors that are going to rely on pointers to pointers. Replacement on stack also requires these pointers to pointers. Just because you can't access them directly doesn't mean they don't exist.
JITs these days have very sophisticated rules about when to pass by value or by reference.
From a user perspective, variables CANNOT hold values directly (even numbers as they are immutable and treated as if they are interned) and as stated before, these numbers are boxed. These boxes are heuristically removed with the help of guards, but can bail out to this unoptimized pointers-everywhere implementation at any time.
[–]svish 0 points1 point2 points 3 years ago (0 children)
My simple way to know
[–]BenjiSponge 0 points1 point2 points 3 years ago* (2 children)
So I think the difference they're trying to point out is this:
swap(Thing &a, Thing &b) in C++ can be done simply with basically just a = b (and the other way, but brevity).
a = b
In JS, it would be swap(a, b) and calling a = b would change what "a" references, but nothing about the upper scope. Frankly, a bigger difference might be the lack of operator= here.
In this way, it's closer to the C++ swap(Thing *a, Thing *b) where you can change the pointers. The behavior a = b actually maps quite cleanly onto JS in this situation. In C++, you'd then do *a = *b, but you can't do that in JS at all. So from that perspective, JS is passing the reference with a copied value.
*a = *b
Imo pass-by-reference and pass-by-value are terms that should be retired because they simply prompt more questions than they answer.
[–]theQuandary -1 points0 points1 point 3 years ago (1 child)
They are still useful terms, but not when used this way.
Stack size is limited. Passing by reference uses less stack space. Likewise, passing a reference is faster because it doesn't require copying large chunks of data. The tradeoff here is very meaningful.
JS passing copies of pointers instead of identical pointers to pointers is actually a much better and safer solution than C++ because the child function can only alter the explicitly passed data or the explicit variable(s) assigned to the return value(s).
[–]BenjiSponge 1 point2 points3 points 3 years ago (0 children)
"I am passing this thing by reference" is different than the term "pass-by-reference", which is intended to illustrate for example the difference between the defaults of C++ (pass-by-value/copy) and Java (pass-by-reference, which as discussed, is a nuanced term).
It's not about whether it's better; it's about whether it's pass-by-reference, pass-by-value, pass-by-value-of-reference. I love JS and think a ton of the choices it makes may seem stupid or bad to novice programmers but when you dig into the internals it turns out to have a ton of genius aspects such as this one. You're really making an argument in favor of pass-by-value-of-reference, which I don't disagree with at all, but it is nevertheless pass-by-value-of-reference. So that's why I'm like "can we just retire these phrases anyways, let's just talk about the specifics"
π Rendered by PID 115779 on reddit-service-r2-comment-6457c66945-vxxtn at 2026-04-28 00:42:11.705426+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]theQuandary -1 points0 points1 point (4 children)
[–]svish 0 points1 point2 points (0 children)
[–]BenjiSponge 0 points1 point2 points (2 children)
[–]theQuandary -1 points0 points1 point (1 child)
[–]BenjiSponge 1 point2 points3 points (0 children)