Sobes.tech
Middle+

/* * Given three code sections. Write the output 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 variable `n` is passed by value. Inside the function `F`, the local `n` is changed to 3, but the external `n` remains unchanged.

```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 the property a inside the function modifies the original object.

var obj = { a: 1 };
function F2(o) {
  o = { hello: 1 };
}
F2(obj);
console.log(obj); // { a: 1 }

In the function F2, the variable o is assigned a new object, but this is a local variable, and the original object remains unchanged.

/* * Given three code sections. Write the output of… - sobes.tech