Middle
A practical task on reference data types: there is an object and a function that manipulates it. What will be printed in three console.log statements?
sobes.tech AI
Answer from AI
If a function takes an object and modifies its properties, these changes will be visible outside as well, since objects are passed by reference.
Example:
const obj = { value: 1 };
function modify(o) {
o.value = 2;
console.log("inside function:", o.value); // 2
}
console.log("before function:", obj.value); // 1
modify(obj);
console.log("after function:", obj.value); // 2
The output will be:
before function: 1
inside function: 2
after function: 2
This happens because obj and the parameter o point to the same object in memory. Changes inside the function are reflected on the original object.