QUESTION 12 / 20 · ARRAYS · INTERMEDIATE

What push gives back

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

Where this matters

When adding an item to a list, keep the mutated array separate from the return value of push. Assigning that return value elsewhere gives a length.

JavaScript · read-only example
const values = [1, 2];
console.log(values.push(3));
console.log(JSON.stringify(values));
Your prediction

Read the full explanation

Push mutates the array and returns the new length, which is three. The second log shows the mutated array.

Trace the code

  1. The original array has two entries.
  2. push appends 3 to the existing array and returns its new length.
  3. The next log serializes that same array after the mutation.

Why the other predictions differ

  • 3 [1,2] — Push appends to the original array even when it is bound with const.
  • [1,2,3] [1,2,3] — Push returns a number, not the array itself.
Expected console output
3
[1,2,3]

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

Change one thing

Append a different number to show that the return value is the length, not the appended item.

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 values = [1, 2]; console.log(values.push(99)); console.log(JSON.stringify(values));
Check the variation’s output
3
[1,2,99]

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