JS Built-ins
← Array46 / 226
ArrayArray.prototype.copyWithinintermediate

Array.prototype.copyWithin() β€” returns the same array

πŸ“– Usage Example
const arr = [1, 2, 3]
const result = arr.copyWithin(0, 1)
console.log(result === arr)  // β†’ true  (same reference)

copyWithin() mutates the array in place and returns the same array reference.

Array.prototype.copyWithin() β€” mutation and return value

copyWithin() mutates the original array and returns a reference to that same array (not a copy). This means the returned value and the original variable both point to the same array.

Challenge: Implement isSameArray(arr) that returns true when the result of calling arr.copyWithin(0, 1) is the exact same reference as arr.

isSameArray([1, 2, 3]) // β†’ true
πŸ’‘ Hints (2)
  • β€’ `===` checks reference equality β€” it returns `true` only if both sides are the same object in memory.
  • β€’ Unlike `map` or `filter`, `copyWithin` returns the original array, not a new one.