JavaScript · Middle · Question 56 of 75

What does this code print?

const a = { user: { name: "Ann" } };
const b = { ...a };
b.user.name = "Bob";
console.log(a.user.name);

Explanation

Spread makes a shallow copy: b.user points to the same nested object. Use structuredClone or copy each changed level: { ...a, user: { ...a.user, name: "Bob" } }.

Further reading

Problem with a question

JavaScript · Middle · question 56

Test yourself

Take the JavaScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz