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);

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).

Further reading

Problem with a question

JavaScript · Junior · question 20

Test yourself

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

Take this quiz