JavaScript · Junior · Question 20 of 75
What does this code print?
const a = { n: 1 };
const b = a;
b.n = 2;
console.log(a.n);- ✓Correct answer:
2 - 2
1 - 3
undefined
Explanation
Objects are passed by reference: b points to the same object as a. To get a copy, use spread { ...a } (shallow) or structuredClone(a) (deep).