DEV Community

Discussion on: Troll Hunting 101: JavaScript Passes Objects by Reference

Collapse
 
thomaslevesque profile image
Thomas Levesque

This is interesting.

I don't know a lot about Javascript, I'm primarily a C# developer. In C#, there are basically two kinds of types:

  • value types: primitive types (numeric types, boolean...) and structures. They're called "value types" because the variable directly contains the value. So when you copy one variable into another, the value itself is copied (i.e. "pass by value", although in C# this phrase typically means something else, which I'll come to later)
  • reference types: all other types, e.g. classes, strings, etc. They're called "reference types" because the variable doesn't directly contain the value, it contains a reference. When you copy one variable into another, the reference is copied, it still refers to the same object (i.e. "pass by reference", with the same caveat as earlier).

The thing is, in C# there's also another concept: passing arguments by reference or by value. And the default is to pass arguments by value. If I call a method that accepts an object (which is a reference type), the argument is passed by value... But the argument just contains a reference, so effectively the object is passed by reference. Passing the argument by reference (which is always explicit, using the ref keyword) means that the callee can reassign the argument (i.e. assign a different reference to it), and the caller will see the updated argument.

This confuses beginners to no end, so I find it interesting that this debate also exists in the JavaScript world!

BTW, I think this argument (which you present as being used by trolls) is valid and doesn't change the fact that in practice, objects are passed by reference:

It's just that the value is a reference to another object.

I think seeing it that way actually helps understand how it works: the variable's value isn't the object itself, it's a reference to the object. When you copy that variable to another variable, the reference is passed by value, but it's still a reference to the same object, so for all practical purposes, the object is passed by reference. (Yes, I realize this could be seen as pedantry, but I think it gives an interesting perspective on how things actually work under the hood).

Collapse
 
bytebodger profile image
Adam Nathaniel Davis • Edited

This confuses beginners to no end, so I find it interesting that this debate also exists in the JavaScript world!

I didn't even realize that there was much of a "debate" about this - until I started reading Dev.to articles and noticed the disturbing number of people who feel compelled to jump in every single time they see any writer commit the sin of implying that JS has pass-by-reference.

BTW, I think this argument (which you present as being used by trolls) is valid and doesn't change the fact that in practice, objects are passed by reference:

Agreed. And to be clear, I'm not saying that everyone who discusses the inner workings of JS pass-by-value/reference is a troll. But I've seen numerous cases, where someone references JS pass-by-reference, and one-or-two users feel compelled to litter the comments with a bunch of base refusals that "pass-by-reference" even exists in JS.

I think seeing it that way actually helps understand how it works: the variable's value isn't the object itself, it's a reference to the object.

Totally agree.

(Yes, I realize this could be seen as pedantry, but I think it gives an interesting perspective on how things actually work under the hood).

I don't think you're being pedantic at all. And there's nothing wrong with diving deeply into a language's gears to understand how it's really working. Here's where I've seen this turn into pedantry:

A dev copies an object into a new variable, alters some of the keys/values inside the new variable, and is then surprised to find the original object has been altered. When someone explains that this is because objects are passed by reference, some Snark Lord jumps in to "correct" them because, "JS always passes by value".

In most scenarios, if I say that this variable holds a reference, and someone tries to correct me by saying that the variable holds "a value - which holds a reference" - that someone is probably being a jerk. Even worse, they're probably making the subject much more confusing to anyone else who doesn't have a firm grasp of the underlying mechanics.

Imagine if you tell me that your couch is in your living room. And I decide that I should "correct" you by stating that there's an area in your living room, which contains the couch.

It's basically like having that guy in the room who feels compelled to constantly correct minor details in everyone else's conversation by butting in with, "Actually..."