r/javascript • • Jun 18 '17

Pass by reference !== pass by value

3.3k Upvotes

272 comments sorted by

View all comments

Show parent comments

13

u/[deleted] Jun 18 '17

Technically everything in JS is passed by reference. Primitives are simply immutable, so it doesn't matter how they're passed - the effect is the same.

5

u/tutorial_police Jun 19 '17

Technically everything in JS is passed by reference.

What definition of "pass by reference" do you use when saying this? Where do you source that claim?

I ask this, because "pass-by-reference" has an established meaning in different programming languages, namely C++, C#, PHP, Pascal and probably others that I know nothing about.

In these languages, pass-by-reference describes semantics that JavaScript does not have.

Namely, you can implement a swap function, call it with swap(a, b); and the variables will have switched values. You can't do this in JavaScript.

So I wonder, why call JavaScript call-by-reference?

-1

u/[deleted] Jun 19 '17

Namely, you can implement a swap function, call it with swap(a, b); and the variables will have switched values. You can't do this in JavaScript.

So I wonder, why call JavaScript call-by-reference?

Holy mother of bike-shedding, people really like to argue about this stuff, huh? :-)

Take a step back and think of a language which is exclusively pass-by-reference and never pass-by-value would be able to implement the swap() function above. Answer is you wouldn't be, because every assignment will be assigning a new reference to the variable, instead of assigning a value to the previous reference. What makes swap(a, b) possible, isn't merely the presence of pass-by-reference semantics, but the simultaneous support of both types of semantics.

Of course, intuitively when people say "pass-by-reference" they expect the situation found in C++, Pascal and so on. So sure, it's not intuitive to call JavaScript "pass-by-reference".

But it's also not intuitive to call it "pass-by-value", because the only thing you can pass by value is references. Again if we'll appeal to intuition, when we say "pass-by-value" we expect we have various values which we can pass-by-value, instead of just references. References to numbers, references to strings, references to functions, references to objects... References, references, references. JavaScript never gives you a value to work with, you always only have access to the reference to pass around.

So strictly speaking, JavaScript is a "exclusively-pass-references-by-value-only" language. Kind of a mouthful ain't it? But it's accurate, so enjoy saying this every time.

1

u/MoTTs_ Jun 19 '17

the only thing you can pass by value is references. ... References to numbers, references to strings, references to functions, references to objects... References, references, references.

The question /u/tutorial_police asked, and that I've also asked you a couple times is: Where do you source that claim?

It's traditionally understood that primitives pass by value and objects pass by reference-value. So what did you read or hear that led you to believe that even primitives pass by reference-value?

→ More replies (3)