Question Bank

JavaScript Array Method Output Traces

Difficulty: Medium

Six traces drilling on in-place vs returning array methods: `reverse`, sparse arrays, `indexOf`, `filter(Boolean)`, default lexicographic `sort`, and `copyWithin`.

Question Bank
/

JavaScript Array Method Output Traces

JavaScript Array Method Output Traces

Six traces drilling on in-place vs returning array methods: `reverse`, sparse arrays, `indexOf`, `filter(Boolean)`, default lexicographic `sort`, and `copyWithin`.

Question Bank
Medium
JavaScript
6 questions
quiz
arrays
array-manipulation-patterns
interview-prep

193 views

6

What do both console.log calls print, and why does mutating arr2 also mutate arr1?

Examples

Example 1:

Input: log(arr1); log(arr2);
Output: ['n', 'h', 'o', 'j']; ['n', 'h', 'o', 'j']
Explanation: Array.prototype.reverse mutates the original array in place and returns the same reference, so arr1 and arr2 point to the same reversed array.