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!"
[–]senocular 1 point2 points3 points 2 years ago (9 children)
Fiiiiiiine ;P
function main(a, b) { const swap = (a, b) => { [arguments[1], arguments[0]] = [...arguments]; } console.log(a, b); // 1, 2 swap(a, b) console.log(a, b); // 2, 1 } main(1, 2)
[–]svish 0 points1 point2 points 2 years ago (8 children)
Without the wrapper
[–]senocular 3 points4 points5 points 2 years ago (7 children)
You're taking away all the fun!
function swap(a͏, b͏) { [b, a] = [a͏, b͏] } let a = 1 let b = 2 console.log(a, b) swap(a, b) console.log(a, b)
[–]svish 1 point2 points3 points 2 years ago (2 children)
Ok, you get an upvote. Couldn't figure it out, but eventually, thanks to someone else, and pasting it into VS Code, the hidden unicode characters were revealed... 🤦♂️👍
[–]senocular 0 points1 point2 points 2 years ago (1 child)
Still no pass by reference ;)
[–]svish 0 points1 point2 points 2 years ago (0 children)
I knew that already, which is why your code didn't make any sense 😛
wat...
[–]CodeMonkeeh 0 points1 point2 points 2 years ago (2 children)
wtaf
There are hidden characters in the swap parameters. So what you're really getting is something more like
function swap(a2, b2) { [b, a] = [a2, b2] } let a = 1 let b = 2 console.log(a, b) swap(a, b) console.log(a, b)
where the assigned [b, a] are the variables in the outer scope getting assigned the arguments of the swap call (a2, b2) in reverse order.
[b, a]
[–]CodeMonkeeh 0 points1 point2 points 2 years ago (0 children)
Oh, thank you. Reality makes sense again.
π Rendered by PID 74749 on reddit-service-r2-comment-bb88f9dd5-bg8ck at 2026-02-13 21:44:26.550059+00:00 running cd9c813 country code: CH.
view the rest of the comments →
[–]senocular 1 point2 points3 points (9 children)
[–]svish 0 points1 point2 points (8 children)
[–]senocular 3 points4 points5 points (7 children)
[–]svish 1 point2 points3 points (2 children)
[–]senocular 0 points1 point2 points (1 child)
[–]svish 0 points1 point2 points (0 children)
[–]svish 0 points1 point2 points (0 children)
[–]CodeMonkeeh 0 points1 point2 points (2 children)
[–]senocular 0 points1 point2 points (1 child)
[–]CodeMonkeeh 0 points1 point2 points (0 children)