TypedArray.prototype.with()

The with() 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 method has the same algorithm as Array.prototype.with(). TypedArray is one of the typed array types here.

Syntax

array.with(index, value)

Parameters

index

Zero-based index at which to change the array, converted to an integer.

  • Negative index counts back from the end of the array — if start < 0, start + array.length is used.
  • If start is omitted, 0 is used.
  • If the index, with negative values counted backwards, is out of bounds, a RangeError is thrown.
value

Any value to be assigned to the given index.

Return value

A new typed array with the element at index replaced with value.

Exceptions

RangeError

Thrown if index > array.length or index < -array.length.

Examples

Using with()

const arr = new Uint8Array([1, 2, 3, 4, 5]);
console.log(arr.with(2, 6)); // Uint8Array [1, 2, 6, 4, 5]
console.log(arr); // Uint8Array [1, 2, 3, 4, 5]

Specifications

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.with

Browser compatibility

BCD tables only load in the browser

See also