Primitive vs Compound Values
So, technically, Primitive values are always Pass-by-value (copied) and Compound values are Pass-by-reference. Here is an example:
But, for Compound values, it would be something like this:
And there’s always the exceptions…
Wait… Why are the values different now? Weren’t Objects and Arrays supposed to be referencing each other?
When you assign a hole new value to the variable, you are changing the reference itself and it doesn’t affect the original Object/Array!
How to fix this?
Let’s say you want to update the properties of your Object and mess up with your Array without worrying about the original value itself.
To fix this, make a copy instead! Here is the code (be aware of the methods available just for ES6 version):
Now you don’t need to worry about the original values! Use this solution whenever you want to copy two original values and later on they can differ from each other.