Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is JavaScript Pass by Reference? #36

Open
AleksandrHovhannisyan opened this issue May 23, 2020 · 8 comments
Open

Is JavaScript Pass by Reference? #36

AleksandrHovhannisyan opened this issue May 23, 2020 · 8 comments
Labels
comments Comments section for an article.

Comments

@AleksandrHovhannisyan
Copy link
Owner

No description provided.

@aidane1
Copy link

aidane1 commented Jul 24, 2020

Very high quality blog. Thank you for the information.

@AleksandrHovhannisyan AleksandrHovhannisyan added the comments Comments section for an article. label Apr 18, 2021
@omonkulov
Copy link

Amazing! Thank you!

@jaehyi
Copy link

jaehyi commented Jun 13, 2021

This is THE best, in-depth explanation I have read on passing by reference and by value--thank you so much!

@Hail91
Copy link

Hail91 commented Jul 15, 2021

Amazing explanation of a pretty tough concept for most. Well done, this is 100% going in my bookmark list to refer back to!

@JackYo
Copy link

JackYo commented May 17, 2022

I can not understand the ambiguous term "pass-by-reference" until I read this blog! Thank you!

@repiso-piyush
Copy link

cleared my misconception

@Goteii
Copy link

Goteii commented Jun 23, 2023

Great article, I could've lost my mind and got that wrong but..
"We’ve learned what references really are: aliases. And as it turns out, this is precisely the kind of reference that we’re talking about when we say a language has “pass by reference.” It’s all about aliasing arguments that were passed in!...." wouldn't that mean that IF javascript would be "pass-by-reference" and we would pass obj as an argument to a function which simply mutates that object's property "name" then we would mutate original object since the argument was just an alias of that object(simillar case to example of "swap" method in C++). Sorry for silly question but the last image in this article confused me

@AleksandrHovhannisyan
Copy link
Owner Author

AleksandrHovhannisyan commented Jun 24, 2023

@Goteii Not a silly question!

IF javascript would be "pass-by-reference" and we would pass obj as an argument to a function which simply mutates that object's property "name" then we would mutate original object since the argument was just an alias of that object(simillar case to example of "swap" method in C++)

No, the scenario you describe is how JavaScript currently works: If you pass an object to a function, you can access and mutate any of its properties, and the change will be reflected outside the function. If JavaScript did support passing by reference, we could change an object reference to point to an entirely new object inside a function, and that change would be reflected outside the scope of the function:

const swap = (objectA, objectB) => {
  const tempObject = objectA;
  objectA = objectB;
  objectB = tempObject;
}

const a = { value: 24 };
const b = { value: 42 };
swap(a, b);

If JavaScript supported pass by reference, a would now be { value: 42 } and b would be { value: 24 }. But this is not possible in JavaScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comments Comments section for an article.
Projects
None yet
Development

No branches or pull requests

9 participants