The with() method returns a new array with one element replaced at a given index. The original stays unchanged. This tutorial covers syntax, negative indexes, comparison with bracket assignment, the ES2023 immutable methods family, and five examples.
01
Syntax
with(i, val)
02
New array
Non-mutating
03
Negative i
From end
04
RangeError
Bad index
05
vs arr[i]=
Immutable
06
ES2023
Modern
Fundamentals
Introduction
Updating one slot in an array usually means arr[2] = "new", which mutates the original. In React and other immutable workflows, you need a copy with one change instead. with(index, value) does exactly that in one readable call.
It is part of the ES2023 “change by copy” family alongside toReversed(), toSorted(), and toSpliced().
Concept
Understanding the with() Method
array.with(index, value) shallow-copies the array, sets the resolved index to value, and returns the new array. Length stays the same; no elements are added or removed.
If the index is out of range after resolving negatives, a RangeError is thrown. Unlike at(), which returns undefined for bad reads, with() refuses invalid writes.
💡
Beginner Tip
Always assign the result: const updated = arr.with(1, "x"). Calling arr.with(1, "x") alone without using the return value leaves arr unchanged—which is correct, but easy to forget.
Foundation
📝 Syntax
General form of Array.prototype.with:
JavaScript
array.with(index, value)
Parameters
index — position to replace (negative counts from end).
with() cannot grow the array. To append, use push, spread, or concat instead.
Applications
🚀 Common Use Cases
React state — setItems(items.with(i, newVal)).
Undo stacks — keep prior array version intact.
Immutable configs — tweak one setting in a list.
Game boards — update one cell in a grid array.
Form rows — replace one field value in an array of inputs.
Functional style — chain with other non-mutating methods.
🧠 How with() Runs
1
Resolve index
Negative? Count from length.
Validate
2
Shallow copy
New array, same length.
Copy
3
Set one slot
Replace value at index.
Replace
4
✅
Return new array
Original untouched.
Important
📝 Notes
Does not mutate the original array.
Returns a new array (assign the result).
Shallow copy—nested objects are shared.
Invalid index throws RangeError.
Does not change array length.
ES2023—use spread/slice fallback for older browsers.
Not the same as JavaScript’s deprecated with statement.
Compatibility
Browser & Runtime Support
Array.prototype.with() was added in ES2023 as part of the immutable array methods proposal (toReversed, toSorted, toSpliced, with).
✓ 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 ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.with()Good
Bottom line: Use spread + slice for older targets: [...arr.slice(0, i), val, ...arr.slice(i + 1)].
Wrap Up
Conclusion
with() is the clean, immutable way to replace one array element by index. It pairs with other ES2023 copy methods and fits modern state management where originals must stay unchanged.
You have reached the last instance method in this series—return to the array methods overview to review the full toolkit.
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 (2023). It works in Chrome 110+, Firefox 115+, Safari 16+, Edge 110+, and Node.js 20+. Use spread/slice fallback for older environments.
Did you know?
JavaScript once had a withstatement (now forbidden in strict mode). Array.prototype.with() is unrelated—it is a safe, modern method name for immutable index replacement.