How useful is this variable assignment exercise?

I came across this exercise, and just wanted to get other peoples thoughts on it. It’s specifically in relation to the Laurel and Hardy exercise. The variable assignment puzzle looks like a loop but seems to be teaching you something about assignment convention/method.

I wouldn’t mind checking if my understanding of the concept is correct so if anyone can help me that would be great.

Thanks everyone!

https://www.jshero.net/en/koans/jsx03.html

Here is an article about this topic:

For newcomers very simple:
Any information is stored in memory. It is about how Javascript stores/references to Information.

1 Like

Thanks for the link @tb47

let name1 = 'Olga';
let name2 = name1;
name1 = 'Clara';
console.log(name2)

Result:

Olga

Eventhough you declared name1 = Clara in line3.

In some other languages … the result would be:

Clara

Related problems regarding this topic/issue can come up time after time.
As mentioned in my first post it is related to how a certain programming languages stores/references to Information in the memory

Keyword: “Pointer/Memory Pointer” - Does name2 reference to the block in the memory in which name1 is saved or does it copy the value during the declaration and so generates a new entry in the memory.

1 Like

Keyword: “Pointer/Memory Pointer” - Does name2 reference to the block in the memory in which name1 is saved or does it copy the value during the declaration and so generates a new entry in the memory.

My understanding was this…

Understanding before

When variable a is assigned the value of variable b. The value of variable a will equal the value of variable b at the point of distribution. When b shares it’s properties with a, A’s properties will not change even if b’s properties do.

This seemed to work for the js-hero example.

But in the article it mentions

assign-by-value or assign-by-reference

Firstly, the variable batman is initialized and when the variable superman is assigned with the value stored in the variable batman , it creates a new copy of the value and stores it. When the variable superman is modified, variable batman is left unaffected as they point to distinct values.

Understanding now

When variable a is assigned the value of variable b. The value of variable a will equal the copy of variable b. A’s properties will not change even if b’s properties do, because A took a copy of B’s properties which are seperate from the present B properties.

Except for this doesn’t apply to objects or arrays, where referencing DOES take place and therefore no copies are created. With every change of the object or array, any variables that have been assigned to said objects/arrays will also be changed.

Have I understood that all correctly?

1 Like

So, when you reassign a variable, it copies the value. Always. That’s how that part of JS works.

If you have an object, the thing the variable points to is the reference: it’s not the whole thing you write in the code, it’s just a value that says “this is an object and it has these properties and these properties are stored somewhere”. So if you reassign it, it still copies it. It is just a value, and it still gets copied in exactly the same way as a number or a string.

But the contents of the object aren’t stored in the same place. If you copy the reference to that object, it will still point at the same contents. If you wanted copies of the contents you would need to copy the contents individually as well, they are seperate things.

It’s a sense it’s like a physical bag. It’s still the same bag regardless of what items you put into it, it doesn’t change into a new bag every time you put something into it or take something out. The contents are seperate things to the bag itself. (Edit: this analogy is really bad in context now I think about it. Sorry! It’s like a bag that can be cloned, but if you clone it the contents don’t clone, they’re the same exact contents, and that doesn’t really make sense :thinking::grimacing::upside_down_face:)

3 Likes

An admirable effort at an analogy there :grinning_face_with_smiling_eyes:

I guess analogies can go all over the shop when describing data storage and principles haha.

1 Like

I think the “assign by reference” is called a “pointer” in languages like C++. As in: it’s just pointing toward the memory-adress where the actual object is saved.
Ofcourse if you copy the pointer, you just have another pointer.

Kinda like the table-of-content of a book: if you copy that, you don’t have another book, you just have another table-of-content. Because it’s only pointing toward the place in the book, where the actual content is.

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.