Passing by reference copying in JS

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.

@MarkoN95 @camperextraordinaire

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”

2 Likes

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

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.

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?

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

As @camperextraordinaire 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.

1 Like

thanks for the reply!

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

oh… i see… (20 characters)

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.