I have a very hard time with this topic.
For example at this code:
let test = "primitive";
const arr = [test];
console.log(arr);
test = "changedPrimitive";
console.log(test, arr);
I can't understand why the test variables value changed but the object(array) remained with the old string
Can someone explain me how this 2 types are stored analytically(memory heap, stack, memory address,value..) and how they behave when we are creating copies(from one variable to another).
For example
let newyear = 2000;
let year = newyear;
newyear = 2022;
console.log(year, newyear);
When copying a primitive value from what i understood reading some articles is that: newyear variable points to a new memory address in the stack which holds the value 2000 and when we create year variable it points to the same address that why the both hold the value 2000, finally when we change the newyear to 2022 a new address is created with that value. While from others i read that the 2 variables have nothing to do with each other, so year does not point to the address of newyear.. They value is just copied from one variable to another..