TypedArray.prototype.with() - JavaScript | MDN

Syntax

js

arrayInstance.with(index, value)

Parameters

index

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

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.

Description

See Array.prototype.with() for more details. This method is not generic and can only be called on typed array instances.

Examples

Using with()

js

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® 2027 Language Specification
# sec-%typedarray%.prototype.with

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on by MDN contributors.