Standard built-ins

Array.prototype.with()

Try the free JavaScript compiler

Returns a shallow copy with one position replaced. Negative indices count from the end; out-of-range indices throw RangeError. Ordinary holes in the other positions become undefined.

Formal Syntax & Structure

javascript syntaxjavascript
array.with(index, value)

Parameters & Attributes

NameTypeDescription
indexnumberRelative index, converted to an integer.
valueanyReplacement value.

Return Value

Array

New array of the same length.

Working Example

Example Snippetjavascript
console.log([1,2,3].with(-1,7).join(","));
JavaScript Reference