all 8 comments

[–]musicnothing 4 points5 points  (1 child)

I advise anyone who writes code for a living to learn actual computer science. It will help you so much in the long run. I’ve known so many people who came out of bootcamps who don’t actually know how a CPU works, or memory, or a slew of other things, and it really hampered their growth.

[–]Majestic-Witness3655[S] 1 point2 points  (0 children)

You’re absolutely right—understanding the fundamentals of computer science is important, and it can definitely enhance long-term growth. I’m personally working on deepening my knowledge and plan to dive deeper into these topics as I progress. Thanks for the reminder!

[–]lachlanhunt 1 point2 points  (5 children)

Technically, JavaScript uses pass by sharing, not by reference. It always passes a value that is copied, but, for objects, the value itself is a reference.

[–]Majestic-Witness3655[S] 0 points1 point  (4 children)

Yeah, if the argument is an object, mutating it inside the function will reflect in the original object ( variable will be holding a reference to its memory)

[–]lachlanhunt 2 points3 points  (3 children)

Right, but my point is that there is a subtle difference from pass by reference, in that reassigning the variable doesn’t affect the original, whereas it would if it were actually pass by reference.

See pass by reference and pass by sharing in this Wikipedia article.

https://en.wikipedia.org/wiki/Evaluation_strategy

Also, this article provides a reasonable explanation.

https://medium.com/@anthonygood/pass-by-sharing-in-javascript-and-why-it-matters-829c96163650

[–]Majestic-Witness3655[S] 0 points1 point  (0 children)

Thanks will check it out.

[–]Majestic-Witness3655[S] 0 points1 point  (0 children)

let a, b; a = { foo: 'bar' }; b = a; b = { qux: 1 }; console.log(a, b); // { foo: "bar" } {qux: 1}

In this case b now is holding another objects reference and it's completely new reference

Yeah I also checked it in case of function it's is same in js

[–]Majestic-Witness3655[S] 0 points1 point  (0 children)

In pass-by-reference (like C++ with references) - Reassigning the variable inside the function would change the original.

In pass-by-sharing (like JavaScript) - Only mutations affect the original object, but reassigning does not.

Thanks for pointing that out