QUESTION 13 / 20 · OBJECTS · BEGINNER

Two paths, one object

What will this code print? Read each line, then make your prediction.

Where this matters

Passing an object between parts of a program can share mutable data. A second variable name does not provide an independent snapshot.

JavaScript · read-only example
const first = { points: 1 };
const second = first;
second.points = 4;
console.log(first.points);
Your prediction

Read the full explanation

Both bindings refer to the same object. Mutation through second is visible through first.

Trace the code

  1. first refers to an object with points set to 1.
  2. Assigning first to second copies the reference to that object.
  3. Changing points through second changes the object that first also reads.

Why the other predictions differ

  • 1 — Assignment copies the reference, not the object.
  • TypeError — Const permits changing a property on the existing object.
Expected console output
4

Reading this explanation does not record mastery. A correct submitted attempt can do so, including after review.

Change one thing

Make a new outer object using spread before changing its numeric property.

Predict the result, then run this self-contained snippet in your browser’s developer console. It uses only sample values and makes no network requests.

const first = { points: 1 }; const second = { ...first }; second.points = 4; console.log(first.points);
Check the variation’s output
1

Explain which binding, value, or operation changed before moving to the next challenge.