Identifies the type of array elements
A new array with the element at index replaced, every other index is read from theArray so an
empty slot of a sparse array is undefined in the new array
RangeError if the index is out of bounds, or the length is longer than an array can be (2^32 - 1)
arrWith([1, 2, 3], 1, 5); // [1, 5, 3]
arrWith([1, 2, 3], -1, 5); // [1, 2, 5]
arrWith([1, 2, 3], -2, 5); // [1, 5, 3]
arrWith([1, 2, 3], 10, 5); // RangeError (out of bounds)
const original = [1, 2, 3];
const modified = arrWith(original, 1, 9);
// original: [1, 2, 3], modified: [1, 9, 3]
// Array-like objects
arrWith({ length: 3, 0: "a", 1: "b", 2: "c" }, 1, "x"); // ["a", "x", "c"]
The arrWith() method is the copying version of using the bracket notation to change the value of a given index. It returns a new array with the element at the given index replaced with the given value. This is an ES2023 feature with polyfill support for older environments.