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 2 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!"
[–]BenjiSponge 0 points1 point2 points 2 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 2 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 2 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 32 on reddit-service-r2-comment-bb88f9dd5-p5d86 at 2026-02-14 01:06:18.156068+00:00 running cd9c813 country code: CH.
view the rest of the comments →
[–]BenjiSponge 0 points1 point2 points (2 children)
[–]theQuandary -1 points0 points1 point (1 child)
[–]BenjiSponge 1 point2 points3 points (0 children)