What is the output of the following code? [[0, 1], [2, 3]].reduce( (acc, cur) => { return acc.concat(cur); }, [1, 2]);

Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

The provided code snippet uses the reduce method to flatten an array of arrays ([[0, 1], [2, 3]]) while also incorporating an initial accumulator value, which is [1, 2].

In detail, the reduce function iterates through each element of the outer array. During each iteration, it takes the current value (cur) and concatenates it to the accumulator (acc).

  1. Initially, the accumulator starts as [1, 2].
  2. In the first iteration, cur is [0, 1]. The accumulator becomes [1, 2].concat([0, 1]), resulting in [1, 2, 0, 1].
  3. In the second iteration, cur is [2, 3]. The accumulator now is [1, 2, 0, 1].concat([2, 3]), yielding [1, 2, 0, 1, 2, 3].

This results in the final output being [1, 2, 0, 1, 2, 3], which aligns with the answer provided.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy