JS Built-ins
← Array217 / 226
ArrayArray.prototype.withbeginner

Replace Element at Index

πŸ“– Usage Example
const arr = [1, 2, 3, 4]
arr.with(1, 99)  // β†’ [1, 99, 3, 4]
arr              // β†’ [1, 2, 3, 4]  (unchanged)

Use with(index, value) to return a new array with one element replaced, leaving the original intact.

Array.prototype.with

The with() method returns a new array with the element at the given index replaced by the provided value.

Implement replaceAt that returns arr.with(index, val).

πŸ’‘ Hints (2)
  • β€’ `Array.prototype.with` was introduced in ES2023.
  • β€’ It returns a new array β€” the original is not modified.