QUESTION 09 / 20 · ARRAYS · BEGINNER

Double without replacing

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

Where this matters

Use map when you want one transformed result for each input, such as converting a list of prices into discounted prices.

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

Read the full explanation

Map builds a new array from callback returns. This callback computes numbers without mutating the input.

Trace the code

  1. The callback runs for each number and returns its doubled value.
  2. Those returned values become entries in a new array, in input order.
  3. The original entries remain unchanged because the callback only computes numbers. A callback that mutates an object could still change shared data.

Why the other predictions differ

  • [1,2,3] [1,2,3] — The new array contains the doubled callback results.
  • [2,4,6] [2,4,6] — Map does not replace the original array’s number entries.
Expected console output
[2,4,6]
[1,2,3]

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

Change one thing

Change the transformation to add one and compare the new array with the input.

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

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