Sobes.tech
Middle+

let obj = { a: 1 }; let array = [obj]; obj = null; console.log(obj); // console.log(array); //

sobes.tech AI

Answer from AI

javascript let obj = { a: 1 }; let array = [obj]; obj = null; console.log(obj); // null console.log(array); // [{ a: 1 }]


Explanation:

- Initially, `obj` references the object `{ a: 1 }`.
- The array `array` contains a reference to the same object.
- When we assign `obj = null;`, the variable `obj` no longer references the object, but the object still exists in memory because `array[0]` references it.
- Therefore, `console.log(obj);` outputs `null`, and `console.log(array);` outputs the array with the object `{ a: 1 }`.

This demonstrates that objects in JavaScript are passed and stored by reference, and changing the variable's reference does not affect other references to the same object.