~ Home 🏡

Understanding the with() Method in JavaScript Arrays

[Mar 14, 25]

The with method in JavaScript is an immutable array method that returns a new array with a specified element replaced at a given index. It does not modify the original array.


Syntax

array.with(index, value);

index is the position of the element to replace, and value is the new element.

Note: If the index is out of bounds, it throws a RangeError.


Example

const array = [1, 2, 3];
const newArray = array.with(1, 4);

console.log(array); // 1, 2, 3
console.log(newArray); // 1, 4, 3