JavaScript Array with() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

Array.prototype.with(index, value) returns a new array with the element at index replaced by value. The original array stays unchanged. Negative indexes count from the end—like at().

Remember
array.with(index, value)  →  new array (original untouched)

with(2, "pineapple")  → replace index 2
with(-1, "z")         → replace last element

arr[i] = x   → mutates in place
arr.with(i, x) → new copy

out of range → RangeError (does not grow the array)

Syntax

Call array.with. Start with this line.

JavaScript
array.with(index, value)
CallWhat it does
array.with(index, value)Primary signature.
array.with(...)Call with the arguments your case needs.

Built into JavaScript. Call with(...) on an array instance — no import required.

Library declarations
JavaScript
array.with(index, value)

Parameters

ParameterDescription
indexRequired. Position to replace (negative OK)
valueRequired. New element at that index

Return Value

Return ValueDescription
*New array (shallow copy + one change)

JavaScript Examples

Example 1 — Replace an Index

Swap one element and keep a new array.

JavaScript
const fruits = ["apple", "orange", "banana", "grape"];
const updated = fruits.with(2, "pineapple");

console.log(updated);
console.log(fruits[2]);  // still "banana"
Try It Yourself

How It Works

  1. Start with fruits. Index 2 is "banana".
  2. Call with(2, "pineapple"). Builds a new array with that slot replaced.
  3. updated has pineapple. The copy reflects the change.
  4. Original stays. fruits[2] is still "banana".

Example 2 — Original Unchanged

Prove identity and contents differ after with().

JavaScript
const original = [1, 2, 3, 4, 5];
const modified = original.with(2, 999);

console.log(original);
console.log(modified);
console.log(original === modified);  // false
Try It Yourself

How It Works

  1. Create original. Five numbers.
  2. Replace index 2. New array gets 999 there.
  3. Log both. Original still has 3; modified has 999.
  4. Different objects. === is false—safe for immutable state updates.

Example 3 — Negative Index

Replace the last element with with(-1, …).

JavaScript
const items = ["a", "b", "c"];

console.log(items.with(-1, "z"));
Try It Yourself

How It Works

  1. Three items. Last index is 2.
  2. −1 from the end. Same rule as at(-1).
  3. Replace that slot. "c" becomes "z" in the copy.
  4. Original intact. items is still ["a","b","c"].

Browser & Runtime Support

Array.prototype.with() was added in ES2023 with the other immutable array helpers (toReversed, toSorted, toSpliced).

✓ Baseline · ES2023

Array.prototype.with()

Supported in Chrome 110+, Firefox 115+, Safari 16+, Edge 110+, and Node.js 20+. Not available in Internet Explorer or very old browsers.

92% Modern browsers
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
Array.with() Good

Bottom line: For older targets, feature-detect with if ("with" in Array.prototype) or fall back to [...arr.slice(0, i), val, ...arr.slice(i + 1)].

Frequently Asked Questions

with(index, value) returns a new array with the element at index replaced by value. The original array is not modified.
No. with() always returns a new array. Assign the result to use the updated copy: const next = arr.with(2, 'new').
Yes. Negative index counts from the end, like at(). with(-1, value) replaces the last element.
with() throws a RangeError if the resolved index is less than 0 or greater than or equal to array.length. It does not extend the array.
Bracket assignment mutates the original array in place. with() returns a new array and leaves the original unchanged—ideal for React state and immutable patterns.
with() is ES2023. It works in Chrome 110+, Firefox 115+, Safari 16+, Edge 110+, and Node.js 20+. Use a spread/slice fallback for older environments.

Back to Array Methods

Explore more helpers—from map and filter to the ES2023 copy methods.

Array methods →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful