Passing by reference copying in JS

Passing by reference copying in JS
0.0 0

#1

In YDKJS types and grammar, the author stated there are two ways of passing values in js. One is value passing (when variable holds a primitive type of value) the other one is reference passing. (when variable holds an object).

Here are two code snippets:

var a = 2;
var b = a; // `b` is always a copy of the value in `a`
b++;
a; // 2
b; // 3

var c = [1,2,3];
var d = c; // `d` is a reference to the shared `[1,2,3]` value
d.push( 4 );
c; // [1,2,3,4]
d; // [1,2,3,4]
var a = [1,2,3];
var b = a;
a; // [1,2,3]
b; // [1,2,3]

// later
b = [4,5,6];
a; // [1,2,3]
b; // [4,5,6]

Can I understand it as

  • in the 1st snippet, what we did was changing the value of the primitive value itself. (by adding 4 to the end of it), therefore after the value changed, all of the references changed as well.
  • in the 2nd snippet, we did not change the primitive value at all, we just assigned b to point to another reference value which is [4,5,6].

Also are these understandings correct?

  • Primitive values are like the parent of all its references.
  • In js primitive value cannot be touched. cannot be altered.
  • so we change its references instead.
  • there is no direct link between references.
  • but there is a link between reference and its parent: primitive value
  • if you change the value of reference, its primitive parent will be changed as well and vice versa.

#2

@MarkoN95 @randelldawson


#3

A reference is the location in memory where the variable is stored. The value is the contents of the variable.

Imagine you have your resume as a Google Doc. I ask you to email me your resume.

You could send me a share-link to your Google Doc. Now I can go in and edit it. You have passed it to me by reference. This might be a good idea if you sent it to me to get me to check your spelling. If I’m going to use your resume as a template for my own, you don’t want me replacing your name with mine on your own resume, etc though.

You could also download your resume as a file, and email me that file as an attachment. Now when I go and edit it, I’m editing my own copy. You’ve passed it to me by value.

Pass by value: "Here’s what’s in the variable"
Pass by reference: “Here is the variable itself”


#4

When the value 4 is pushed into d, it is really just pushing to the same array referenced by c, because d is just another reference to the same array.

This has nothing to do with changing primitives. The b = a created another reference to the same array of values that a refers to. Later, with the b = [4, 5, 6], b is assigned a reference to a different array of values.

Primitive values are not referenced.

Not sure what you mean by touched here.

In general, based on most of what you have written, you are not understanding what a primitive is. In JavaScript, there are only 5 primitive types (Boolean, null, undefined, String, and Number). When you assign a primitive value to a variable, the variable contains a copy of that value. When you assign an array to a variable, you are assigning a reference to the where the values are being stored (in memory). The variable does not actually contain the values as in the case of a variable holding a primitive value.


#5

i see… I think I misunderstood some of the concept when it comes to primitive and references…


#6

may I ask that where are the primitives being stored? and where are all the references being stored? In the RAM ? or ?

what i meant is that we can only do things to the copy of primitive values, either a reference copy or a value copy, the original values cannot be altered.


#7
var a = 2;
var b = a;

in this snippet there are 3 value of 2.

1st: the primitive 2
2nd: a copy of 2 that is assigned to a
3rd: a copy of 2 that is assigned to b

is this correct?


#8

i saw this on stackoverflow, I think this is a simpler version of understanding some of this, are these understandings correct?


#9

Yes, you could say that. Only two of them (a and b) are stored in memory (RAM more than likely).


#10

JavaScript does not have pointers like in other languages. They have references.


#11

As @randelldawson indicated, there are other languages (the C family) that have a thing called “pointers”. They’re weird and frustrating and powerful. So be careful about using this terminology when talking about other languages. That said, your post is conceptually correct and if it helps you understand, excellent.


#12

thanks for the reply!

also all the references are stored in the memory… then where is the primitive value being stored??


#13

All are stored in memory.


#14

oh… i see… (20 characters)


#15

i’ve found my old notes about primitives VS references.
And here are some of it, are these understandings correct?

  1. primitive values are data that is stored on the stack. primitive value is stored directly in the location that the variable accesses.
  2. references values are objects that is stored in the heap.

so in the example above , the primitive 2 is in the stack.
2 copied value of 2 are stored in the heap.