Say I make an array (myArray) of objects in Javascript, then I use a variable say selectedObject to keep track of which object is selected.
selectedObject= myArray[3];
Suppose I then add or remove items from myArray (for instance inserting a new item at the start), is my selectedObject still pointing to the same object? Or as in C it’s pointing to the same memory address, so a different object?
It’s still pointing to the same object. Think of myArray as an array of pointers in C: myArray[3] is a reference, not the object itself.
It’s different when myArray is an array of value types (like int, float or even some struct like Vector3). However, in that case selectedObject is a value type as well and the data is copied directly into selectedObject (i.e. references are not involved at all).
myArray=Array("a","b","c","d");
SelectedObject=myArray[3];
//SelectedObject would be "d".
//lets add another element at the start
myArray.Unshift("z");
//At this point, SelectedObject is still "d"
//calling myArray[3] again would now set it to be "c", as the array has gained an extra index
SelectedObject=myArray[3];
//SelectedObject is now C