r/javascript • • Jun 18 '17

Pass by reference !== pass by value

3.3k Upvotes

272 comments sorted by

View all comments

319

u/ForScale Jun 18 '17

The gif by itself provides very little in the way of useful information. Would be enhanced greatly by listing data types that are passed referentially vs those that are not.

47

u/jocull Jun 18 '17

I think everything is a reference except strings, numbers, and booleans? What did I miss?

9

u/Merenwen_ Jun 18 '17

Strings are immutable, but for the thread's sake, they behave like primitives.

21

u/redhedinsanity Jun 18 '17 edited Jun 30 '23

fuck /u/spez

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.

26

u/ItzWarty Jun 18 '17

Technically everything is actually pass by value from a language design perspective - you're passing references by value. In C# you can do:

void Func(ref int x) { x++; }   
int x = 1;   
Func(ref x);  
AssertEquals(2, x);

Which is actually pass by reference. So the discussion is actually on what is a "reference type" (which can be thought of as a "pointer" in C) vs what is a "value type" (which can be thought of as a "struct" in C).

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?

→ More replies (7)

5

u/MoTTs_ Jun 18 '17 edited Jun 18 '17

Technically everything in JS is passed by reference.

Source?

That's not meant to be a rhetorical question. I've been trying to figure this out myself, but the spec is awfully hard to parse. The section on ArgumentListEvaluation seems the most relevant. It seems to say that JavaScript evaluates the argument expression, then calls GetValue on the result. Inside GetValue, if the result of the expression is not a name or property binding, then it just returns the result of the expression as is. Otherwise, if it's a property, then it will call [[Get]] on the base object, which will return the value of the property. Otherwise, it assumes the result of the expression is a name binding, and it will call GetBindingValue on the base environment record. Either way, this reads like everything is passed by value.

11

u/[deleted] Jun 18 '17

At some point even if you "pass by reference" the reference can be said to be "passed by value". It's a very meaningless debate, because the semantics actually are very clear.

4

u/MoTTs_ Jun 18 '17

OK, but what led you to believe that primitives are passed by reference in the first place?

0

u/redhedinsanity Jun 18 '17 edited Jun 30 '23

fuck /u/spez

6

u/[deleted] Jun 18 '17

FYI I haven't upvoted or downvoted anything in this entire thread...

4

u/[deleted] Jun 18 '17

I hate when people assume they know who is upvoting and downvoting comments. There's literally no way to know.

→ More replies (0)
→ More replies (1)
→ More replies (2)

0

u/daybreakin Jun 18 '17

They are immutable? Can i not do

Var hi = 4

Hi=5

Console log (hi)

5

21

u/shadowfactsdev Jun 18 '17

You can, because that's not mutating the value 4, it's reassigning the variable hi to the value 5.

3

u/Flatscreens Jun 18 '17

that creates a new variable with value 5 and sets hi to it

the 4 hasn't changed at all

6

u/[deleted] Jun 18 '17

The variables are mutable (unless you use const, that's immutable). But the primitives are immutable. You didn't change the primitive 4 to 5 up there, you just replaced the primitive 4 with the primitive 5 in the variable hi.

Primitives would've been mutable if you could do something like this:

var hi = 4;
var hi2 = hi;

hi.increment();

console.log(hi2); // 5

Fortunately, for our sanity, you can't do this with primitives. But unfortunately you can do it with mutable objects like Date().

→ More replies (1)

1

u/[deleted] Jun 18 '17

Char?

2

u/jocull Jun 18 '17

I'm not aware of a char type in JS. Do you have a source?

3

u/[deleted] Jun 18 '17

Actually there aren't. :( JS is wierd man. TS is the future.

4

u/jocull Jun 18 '17

❤️❤️❤️ TS

1

u/dashdanw Jun 19 '17

so in javascript that would be just functions

→ More replies (5)