var items = new Array("one", "two", "three");
var itemsRef = items;
items.push("four");
console.debug(itemRef);
console.debug(items);
I really dont get the idea how above items and itemsRef are same even items added “four” when after itemsRef referenced items. Isn’t it reasonable that itemsRef should have only (“one” “two” “three”)?
If itemsRef keep pointing items forever, why do we use such an useless argument like items = itemsRef? I am still not getting the idea. Can anybody tell me how this is works and why JavaScript let variables pointing each other forever?
Javascript makes assignments of Objects and Arrays by reference instead of by value/copy. That’s why you’re seeing that behavior. There’s plenty of web documentation about that.
But if your goal is to copy an array, do this: