DEV Community

Discussion on: Understanding the Difference Between Reference and Value in JavaScript

Collapse
 
val_baca profile image
Valentin Baca • Edited

Sorry, this is wrong.

It is always pass by value. Values of arguments are always copied and sent to the function.

The "value" of an object is the reference. This is why it seems like it's pass-by-reference but JS does not support pass-by-reference.

Yes, you can mutate the object from within the function, but you cannot change the reference.

var x = { /* some obj */ };
var y = x;
foo(x); 
// no matter what foo does, the following will be true. You cannot make x reference any other object.
x === y

x is not the object. x is not the object.

When you call a function with x, you aren't passing x. You're passing the value of x. The value of x happens to be a reference to an object.

Pass-by-value by copying a reference is NOT pass-by-reference.

Pass-by-reference is a concept/feature that (effectively) only exists in C++: learncpp.com/cpp-tutorial/73-passi...

Sources:

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

Agree with above of course. Just want to point out to people coming from a C# world that for instance structs in C# are passed by value. You get a copy of an object with all of its members also copied. It ends up on the stack. class instances are passed by "a copy of a reference" if you like often short-handed to "passed by reference". In Javascript only primitives are passed by copying the pure value, objects are always a copy of a reference.

Collapse
 
val_baca profile image
Valentin Baca • Edited

Correct. structs in several languages behave similarly (C, Go, Crystal). The cost of copying becomes larger as the struct becomes larger, which is where pointers come in. Then references were made as a safer alternative to pointers. (obviously glossing over details here)

By the way, I think you mean instances when you wrote "classes are passed by "a copy of a reference""

In the end it all comes down to knowing how pointers work. So many programmers think that if they're not coding in C or C++ then they can just not learn how pointers work since they're dealing with """references"""

Knowing what is exactly copied is incredibly important in every language.

Thread Thread
 
miketalbot profile image
Mike Talbot ⭐

Ooops... Quite right, instances of classes.

Collapse
 
thawkin3 profile image
Tyler Hawkins

Valentin, thank you for the correction and the detailed articles!

The mental model I've had that "primitives are passed by value; objects are passed by reference" has served me well over the years, and it's been helpful in understanding what behavior to expect, but it appears that I've been using the incorrect terms to explain what's really going on underneath the hood.

I've read through all your linked references. Just for my understanding, it seems like the following statement from the Stack Overflow answer is an appropriate way to explain the concept, right?

Primitives are passed by value. Objects are passed by "copy of a reference".

Or as you've said:

It is always pass by value. The "value" of an object is the reference.