Middle
Given three sections of code. You need to write the result of each console.log. var n = 1; function f(n) { n = 3; } f(n); console.log(n); // ?? // ------------------ var obj = { a: 1 }; function f1(o) { o.a = 5; } f1(obj); console.log(obj); // ?? // ------------------ var obj = { a: 1 }; function f2(o) { o = { hello: 1 }; } f2(obj); console.log(obj); // ??
sobes.tech AI
Answer from AI
js var n = 1;
function f(n) { n = 3; } f(n);
console.log(n); // 1
Here, the parameter `n` of the function `f` is a local copy of the value passed during the call. Changing `n` inside the function does not affect the external variable.
```js
var obj = { a: 1 };
function f1(o) {
o.a = 5;
}
f1(obj);
console.log(obj); // { a: 5 }
Objects are passed by reference, so changing a property of the object inside the function affects the original object.
var obj = { a: 1 };
function f2(o) {
o = { hello: 1 };
}
f2(obj);
console.log(obj); // { a: 1 }
Here, the variable o inside the function is assigned a new object, but this does not change the reference obj outside the function. Therefore, obj remains unchanged.