I am new to JavaScript and try to understand some core concepts.
I created an array and assigned it to the variable fullNames. Then I declared another variable (fullName) and assigned the first (and only) element of fullNames array to that variable.
As I checked, if I modify the first element (which is an object) of fullNames array the value is also modified for element assigned to the fullName variable, which basically means that element wasn't simply copied to fullName variable and that fullName simply points to the first element (object) in fullNames array. Right?
However, if then I remove all elements from an array initially assigned to fullNames variable, the fullName variable to which the first element was assigned still logs this same element.
So, what happens in that case? Why firstName still points to some object if all elements were removed from an array prior I log firstName to console?
let fullNames = [{ name: "Name", surname: "Surname" }];
let fullName = fullNames[0];
fullNames.length = 0;
console.log(fullNames); // displays []
console.log(fullName); // displays { name: "Name", surname: "Surname" }
var a = {x:1}, b = a;. When you do likea = null;then the reference chain gets broken andbremains as originally referenced (instantiated){x:1}.