QUESTION 02 / 20 · SCOPE · BEGINNER

A constant object?

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

Where this matters

A constant binding is useful for keeping the same settings object while allowing its properties to change.

JavaScript · read-only example
const user = { level: 1 };
user.level = 2;
console.log(user.level);
Your prediction

Read the full explanation

Const prevents rebinding user. Updating a property on the same object is permitted.

Trace the code

  1. user refers to one object whose level begins at 1.
  2. The property assignment changes that object; it does not assign a different object to user.
  3. Reading user.level therefore sees the updated property.

Why the other predictions differ

  • 1 — Const does not freeze the object’s properties.
  • TypeError — Reassigning user itself would fail; this statement changes only a property.
Expected console output
2

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

Change one thing

Rebind user to a new object instead. Catch the error so you can compare its kind with a permitted property update.

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 user = { level: 1 }; try { user = { level: 2 }; } catch (error) { console.log(error.name); }
Check the variation’s output
TypeError

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