A review panel wants a new list with one slot replaced. It switches from bracket assignment to Array.prototype.with and discovers that a negative index works differently, an index at the end throws, and a getter on the replaced slot never runs. These are separate consequences of the copying replacement contract.
The short answer is that the ordinary built-in with method resolves a relative index against the source length, rejects an out-of-range result and creates a new array of that length. At the selected position it uses the supplied replacement value instead of reading that source position. At other copied positions it obtains source values in order. Ordinary bracket assignment has a different key and mutation contract.
This guide uses original fictional review lists and controlled array-like fixtures. It concentrates on index resolution and read selection. For a fuller comparison of positional deletion, inserted values and removed-element products, continue into the existing toSpliced lesson. The links connect shared copying boundaries while keeping the two operations distinct.
This specialist guide belongs to the Websites root. It also connects to The Gold Standard of Education | Learning That Transfers into Life and the How X Works hub. The worked cases are original teaching examples; their numbers are stipulated so that the reasoning can be checked.
eduKateSG · Websites
Find your next learning step
Resolve replacement indices, check range failures and trace which source values Array.prototype.with reads. Preserve the distinction from bracket assignment and deep snapshots.
ROUTE 1 · CHAPTERS 1–3
Define the replacement
Compare new results, relative indices and the boundary that excludes appending.
ROUTE 2 · CHAPTERS 4–5
Resolve and select
Keep input grammar separate from numeric conversion and skip the replaced old value.
ROUTE 3 · CHAPTERS 6–8
Trace observable work
Follow retained getters, early range failure and captured extent.
ROUTE 4 · CHAPTERS 9–11
Check the source and result contracts
Locate sparse reads, shared records, subclasses and generic receivers.
ROUTE 5 · CHAPTERS 12–14
Transfer and verify
Rework the archive history and report bounds, actual reads and chosen current list.
Chapter contents
Define the replacement · Chapters 1–3
1. Name the one-position replacement job
Resolve and select · Chapters 4–5
4. Follow index conversion without inventing a field grammar
Trace observable work · Chapters 6–8
6. Read the retained positions in their actual sequence
Check the source and result contracts · Chapters 9–11
9. Connect sparse and inherited positions to the selected read
10. Separate new container identity from record ownership
11. Distinguish result construction from subclass expectations
Transfer and verify · Chapters 12–14
12. Transfer the method to a changed archive review list
13. Build checks around bounds and read selection
14. Report the resolved position and the values actually read
The review panel begins with slot identifiers A12, B24 and C36. It wants a new list containing A12, X80 and C36, while retaining the original list for another view. The selected operation changes one output position without adding or removing a position.
const source = ["A12", "B24", "C36"];
const revised = source.with(1, "X80");
// revised: ["A12", "X80", "C36"]
// source: ["A12", "B24", "C36"]
Keep two array boxes in the first drawing. The source still contains its three original indexed relationships. The returned array has a different outer identity and contains the replacement at index one. The method did not assign X80 into the source's property at that index.
A result-presence check alone is insufficient. Both an in-place assignment and a copying replacement can make some array contain X80. Assert the source after, returned contents, returned length and outer identity separately to identify the intended contract.
The job is replacement, not insertion. A supplied replacement array becomes one element value rather than automatically spreading its members into additional slots. If the application needs a variable-length edit, it needs a suitable different operation and a separately stated argument contract.
For learner practice, write down source before, selected index, supplied replacement, returned array and source after. Then identify which array the panel should display and which array the other view still holds. Constructing a returned value does not automatically update every binding or component that previously referred to the source.
This first example establishes a passive ordinary-array baseline. Active getters, generic receivers and unusual indices will be changed conditions. Beginning with their boundary-specific questions makes the later observations explainable instead of presenting a collection of surprises around a familiar-looking copy operation.
CHAPTER 2 OF 14 · Define the replacement
2. Resolve negative indices against the captured length
For a three-element source, index negative one selects the final position. The method's relative-index rule resolves that value using the source length. It is not the same property key as the literal string minus one under ordinary bracket assignment.
const source = ["A12", "B24", "C36"];
source.with(-1, "X80"); // ["A12", "B24", "X80"]
source[-1] = "note";
source[2]; // "C36"
source[-1]; // "note"
The bracket assignment in this ordinary fixture creates or updates a property with that negative-looking key. It does not gain the with method's relative-index interpretation simply because the same numeric spelling appeared inside brackets.
For a length of three, negative three resolves to the first position, while negative four falls before the available range and is rejected. Work the arithmetic before predicting the returned array: add the negative integer index to the captured length, then test whether the result lies inside the available positions.
Do not infer that every JavaScript array operation interprets negative arguments identically. Some use relative positions, some clamp boundaries and some work with property keys directly. The operation's own contract controls the interpretation.
This matters for a review panel whose user-facing slot numbers begin at one. A displayed slot number and a zero-based method index require an explicit translation. Negative indexing is another specified convention, not a substitute for defining that translation correctly.
For practical tests, include zero, the final nonnegative index, negative one and negative length. Record the original source length and resolved actual position. Then add one value just outside either boundary and predict the failure rather than extending the ordinary examples by intuition.
The strongest explanation keeps the supplied argument and resolved position separate. Saying index minus one changed the last slot omits the length that made that resolution meaningful and can conceal a bug when the same helper later handles a shorter review list.
CHAPTER 3 OF 14 · Define the replacement
3. Treat the boundary as replacement rather than append
An actual index equal to length lies just beyond the existing positions. The with method rejects that result instead of treating it as a request to append. A replacement operation needs an existing position in the captured extent.
For the three-slot source, indices zero, one and two are inside the range. Index three is not. A method call with index three throws a RangeError under the ordinary fixture and does not return a normal revised array.
const source = ["A12", "B24", "C36"];
// source.with(3, "X80") throws RangeError.
source[3] = "X80";
source.length; // 4
The bracket assignment demonstrates a different operation: an ordinary array indexed write can extend the array under its own conditions. It is not a counterexample to the with range rule. The two operations answer different editing questions.
An empty source provides no existing replacement position. Even zero is outside its available replacement range. A helper must decide whether empty input is a valid no-op, an invalid request or a reason to use an explicit insertion path. The built-in method does not silently choose a product policy for that application.
Do not hide the exception by returning the unchanged source without documenting that behaviour. Such a wrapper would implement a different contract, and a caller might mistake an ignored requested replacement for success.
For the review panel, keep the intended slot and current extent attached to an invalid-index report. A list can shrink between selection and application. The relevant recovery might ask the user to select a current item rather than replacing a different slot after arbitrary clamping.
For checks, assert the exact error class, that no normal result was produced and the source state under the passive fixture. Then verify the separate append operation if the feature supports it. A complete test should not collapse replacement rejection and successful list growth into one vaguely defined update behaviour.
CHAPTER 4 OF 14 · Resolve and select
4. Follow index conversion without inventing a field grammar
The built-in index argument undergoes numeric conversion and integer interpretation under its specified relative-index machinery. An application receiving text should not assume that this mechanism is the same as validating the exact spelling permitted in its interface.
For a controlled array, a fractional positive index can be interpreted through truncation toward zero before selecting the actual position. A numeric string can be converted into a number. NaN-like conversion results can select the zero interpretation under the relevant integer-conversion rule.
These are language behaviours to understand, not a recommendation to accept every convertible string in a slot-selection form. A user-facing field can require a complete integer spelling within a declared range before calling the method.
const source = ["A12", "B24", "C36"];
source.with(1.8, "X80"); // ["A12", "X80", "C36"]
source.with("1", "X80"); // ["A12", "X80", "C36"]
An index object can execute code while converting itself. A Symbol index can fail numeric conversion rather than select a position. Infinity and negative Infinity are outside any finite ordinary array's replacement positions. Keep conversion failure separate from a successfully converted index rejected by the range test.
The existing numeric-conversion guide develops complete-field parsing versus permissive numeric prefixes. Continue there for input grammar choices. This lesson follows the already supplied argument through this particular method's conversion, relative resolution and range decision.
For practical tests, include a fractional number, a numeric string, NaN, infinities and one deliberately failing conversion in isolated fixtures. Record the converted interpretation where it completes and the actual position where it is valid.
Do not use a successfully returned array as proof that the visitor entered an accepted slot number. The application must establish the intended grammar and range at its own boundary. The method then performs the replacement according to the supplied argument it actually received.
Compare the read-oriented at method using the same resolved-position idea. With a three-element ordinary source, at negative one obtains the final source value, while with negative one constructs a new array using the supplied replacement at that position. The read operation needs the old selected value; the replacement-copy loop does not. Their shared relative-index convention does not make their selected source reads identical.
An out-of-range at read supplies undefined under its ordinary contract, while an out-of-range with replacement throws RangeError. Consequently, testing whether source.at(index) returns undefined is not complete validation of a replacement position. A valid present position can itself contain undefined, and a missing indexed value can be read as undefined within the available length. Range and obtained content remain separate conditions.
For a concrete exercise, use a source whose final position is present with value undefined. At negative one produces undefined, yet with negative one can validly return a new array containing X80 at that final position. Calling at at the source length also produces undefined, but with at that same length rejects the request. Equal read results conceal different index-validity conditions.
Keep the evidence explicit: captured length, resolved position, whether that position lies within the replacement extent, and the value obtained only when a read is required. This comparison helps a review panel avoid rejecting a valid undefined-valued slot or accepting an out-of-range replacement merely because both read paths appeared to return the same empty-looking value.
CHAPTER 5 OF 14 · Resolve and select
5. Observe that the replaced source value is not read
The most distinctive read-selection rule appears when the selected source position has a getter. The method already has a supplied replacement value for that output position. It uses the replacement instead of obtaining the old source value there.
const log = [];
const source = ["A12", "B24", "C36"];
Object.defineProperty(source, 1, {
get() { log.push("old value read"); return "B24"; },
configurable: true
});
const revised = source.with(1, "X80");
// revised: ["A12", "X80", "C36"]
// log: []
The getter still exists on the source. The empty log does not mean it was removed or converted into a passive data property there. It means this method invocation did not need that selected old value when constructing its returned array.
Change the replacement index to zero while leaving the getter at source index one. Index one is now a retained copied position, so its value is read and the getter marker can appear. The changed selection explains the changed effect.
This gives a strong distinguishing test. A proposed implementation that first copies every source value and only afterwards replaces one result position may invoke the old getter unnecessarily. Its final array can match the built-in result for passive values while its read trace differs under the active fixture.
Do not turn the observation into a universal guarantee that the selected property can never be involved in any earlier work. Argument construction or explicit code outside the method can read it before invocation. The claim concerns the built-in method's own copying loop at the selected position.
For the review panel, this difference can matter when a legacy model exposes active indexed access. Preserve the supported source contract and the actual read trace during a refactor. The correct returned values alone do not establish equivalence when obtaining an old value has observable effects.
CHAPTER 6 OF 14 · Trace observable work
6. Read the retained positions in their actual sequence
At copied positions other than the selected replacement index, the method obtains source values and creates result entries. An accessor at one retained position can therefore affect a later retained value before that later value is read.
Use an array-like fixture with length three. A getter at index zero changes index two from C36 to changed and returns A12. Replace index one with X80. The method reads index zero, uses the supplied replacement at one and later reads the changed value at two.
const source = {
length: 3,
get 0() { this[2] = "changed"; return "A12"; },
1: "B24", 2: "C36"
};
Array.prototype.with.call(source, 1, "X80");
// ["A12", "X80", "changed"]
This is not an atomic snapshot of every source value taken before getters run. The result reflects the specified progression of selected reads and constructions. The word copy describes a new output container, not a suspension of active source behaviour.
Now move the changing getter to the selected replacement position. The built-in copying loop does not read that position, so the getter's planned update to index two does not occur through that loop. A later retained read can therefore obtain the original C36 under the changed fixture.
Keep these examples isolated from global prototype changes or unrelated callbacks. Their purpose is to distinguish read selection and ordering, not to create a production model whose behaviour depends on hidden index effects.
For practical tests, assert the returned values, getter log and relevant source state. The source can change through a getter's own code even though with does not perform the replacement assignment on it. Those are different effect channels.
A reliable explanation names which read caused the source effect and when the later value was obtained. Describing the whole attempt as either mutating or non-mutating without that scope would obscure precisely the condition this fixture exposes.
CHAPTER 7 OF 14 · Trace observable work
7. Place range failure before indexed copy reads
The method obtains the source length and resolves the index before entering its indexed copying loop. If the actual replacement index is out of range, the method throws before it needs to read the indexed source values for that result.
Create an array-like object with length three and getters at zero, one and two that append their indices to a log. Call the method with index three. The invalid actual index causes a RangeError, and those indexed getter markers remain absent under the controlled fixture.
That does not make the entire failed attempt effect-free. Obtaining length can invoke a getter on a generic receiver, and converting the index can execute user-provided conversion code. The failed range check is later than those stages and earlier than the indexed copy reads.
For a precise ledger, use a length getter that logs length and an index object that logs index conversion. The invalid invocation can show those markers without any copied-index markers. This distinguishes an early failure from a method that began copying and then encountered a later getter error.
Argument expressions supplied by the caller are evaluated before the method's body runs. A replacement-building call can therefore execute even when the eventual index is out of range. The range check does not retroactively skip work the language already performed to call the method.
For the review panel, avoid treating the absence of a returned array as proof that every builder or source observation was skipped. Retain the actual stage evidence if retrying could repeat a meaningful external effect.
Tests should assert the exact RangeError, indexed getter count and any earlier deliberate markers. A broad failure assertion alone cannot establish the ordering contract. Conversely, one earlier marker should not be misreported as proof that the method copied part of the array before rejecting the index.
This stage boundary is a useful reason to consult the actual algorithm rather than infer behaviour from a successful dense-array example, where no getter or error makes the sequence visible.
CHAPTER 8 OF 14 · Trace observable work
8. Keep captured extent separate from later source changes
The source length used by the method establishes its copying extent and relative-index calculation for that invocation. A later source change during an indexed getter does not automatically turn that operation into a newly sized copy of every then-current position.
Use a generic receiver whose length initially returns three. During a retained index-zero getter, add a value at index three and change a writable length field to four. The method's captured extent remains three under the controlled fixture, so the added fourth position is not automatically included in the returned array.
The relative replacement index was also resolved against the earlier captured length. If the call supplied negative one, that resolution selected the third position. A later length change does not move that selected replacement to a newly added fourth position within the same invocation.
For paper practice, draw the captured length as a separate value beside the live source object. Mark the actual replacement index after conversion. Then trace the indexed loop using that recorded extent while allowing explicitly demonstrated getters to alter the source state.
This does not imply every copied value is frozen at the same early moment. The previous chapter showed that retained values can reflect later changes before their individual reads. Extent capture and value capture occur at different boundaries.
The distinction matters for a helper accepting active array-like sources. A new array with length three can contain a value changed during construction while excluding a new index added during that same construction. Those results are coherent once the two capture questions are separate.
For ordinary passive arrays, the source may remain stable throughout the call and the distinction is less visible. Keep that useful simple domain clear. A generic library promising broader inputs should test the changed fixture deliberately instead of importing a passive-array intuition into every source.
The practical report records captured extent, resolved index, read order and returned contents. It does not describe a new container as a globally instantaneous view of an active source.
CHAPTER 9 OF 14 · Check the source and result contracts
9. Connect sparse and inherited positions to the selected read
A missing source position outside the replacement index is still among the positions copied through ordinary value reads. Under an ordinary sparse fixture with no relevant inherited indexed property, that read yields undefined and a corresponding result entry is created.
If the missing position is the selected replacement index, the supplied replacement value is used there instead. The method does not first need to obtain an undefined old value merely because the source has a hole at that position.
const source = ["A12", , "C36"];
source.with(1, "X80"); // ["A12", "X80", "C36"]
source.with(2, "X80"); // ["A12", undefined, "X80"]
The existing toSpliced guide gives the deeper hole-versus-undefined and source-presence comparison. Here use that knowledge to ask the distinctive selection question: is the position replaced directly or read as part of the retained copy?
An inherited indexed value is another changed source condition. At a retained position, ordinary lookup can obtain that inherited value. At the selected replacement position, the supplied replacement still takes precedence in the method's copying loop without reading the inherited old value there.
Do not modify Array.prototype globally to demonstrate this. A local array-like receiver with its own controlled prototype can expose the lookup condition without changing unrelated arrays used by other checks.
For practical validation, inspect result property presence where it matters to downstream consumers. A present undefined entry may be invalid as a review record even though the method successfully created it. The copying contract does not certify the meaning of every resulting element.
Keep the explanation attached to read selection rather than repeating a generic sparse-array slogan. A learner should predict the two displayed results by locating the replacement position, identifying the retained reads and applying the stated inherited-property condition at those reads.
CHAPTER 10 OF 14 · Check the source and result contracts
10. Separate new container identity from record ownership
The returned array has a new outer identity, but object-valued elements retained from the source are still references to their objects. A supplied replacement object also becomes that supplied reference in the result. The method does not deep-clone every review record.
For a controlled fixture, put a review record with status pending at source index zero and replace index one. The returned array and source differ as arrays, while their index-zero entries can be the same object. Updating that shared record's status can be observed through both arrays.
That does not mean the method performed an in-place replacement on the source. Its index-zero reference can remain unchanged while the referenced object's field changes later. Container membership and child-record state require separate observations.
The nested-freeze guide develops the corresponding integrity question. Freezing an outer array does not automatically freeze each referenced review record. A copied and frozen container therefore does not by itself establish an independent immutable history.
For a panel needing a historical snapshot, choose a supported copying representation and ownership policy for its records. For a panel deliberately sharing live record objects across different list arrangements, retain that sharing explicitly. The method does not decide which application architecture is appropriate.
A frozen source array can still be read by a copying operation under the ordinary supported conditions, since the replacement is written into the new result rather than assigned to the source's restricted indexed property. That is different from an ordinary attempt to mutate the frozen source at that position.
Test the actual source domain. Accessor behaviour, unsupported objects and specialized collection kinds can add different contracts. An ordinary Array method should not be silently treated as the corresponding TypedArray operation with its separate value-conversion rules.
For the review feature, record which array becomes current, which aliases retain the original array and which child identities remain shared. That ledger connects the method's copied result with the user's actual next view without claiming that one method supplies complete data isolation.
CHAPTER 11 OF 14 · Check the source and result contracts
11. Distinguish result construction from subclass expectations
The ordinary built-in Array with method constructs an ordinary array result rather than applying every custom result-construction policy that an array subclass might use elsewhere. A new result is not necessarily an instance of the receiver's application-specific class.
For a controlled subclass of Array, create three review codes and call the built-in with method. Inspect the returned array's prototype alongside its contents. The source's subclass identity and the returned ordinary array identity answer different questions.
This can matter when a caller expects helper methods or metadata attached to a custom list class. Matching indexed contents do not establish that the result carries that class's additional behaviour. A migration should test the interface the next stage actually consumes.
The method is also generic over suitable array-like receivers: a receiver with a length and indexed properties can be used through the built-in method's call interface. The receiver need not itself be an Array instance for that contract to obtain values and build an ordinary array result.
Generic acceptance broadens the read and conversion questions. A length getter can run, indices can be inherited and active properties can change later source values. A helper documented only for passive ordinary arrays can intentionally keep a narrower supported domain.
Do not let a convenient generic invocation imply that every platform collection or specialized numeric buffer is interchangeable with an ordinary array. Each source has its own property or iteration behaviour, and a different method with the same name can have different value rules.
For learner practice, compare an ordinary array, a simple subclass and a plain array-like object with the same three passive values. Assert the returned ordinary array prototype, contents and unchanged relevant source relationships. Then add one active length or indexed getter as a separate changed condition.
The practical conclusion is a declared input and output interface. The copying replacement provides one result product; preserving custom application behaviour beyond that product requires an explicit construction or adaptation decision rather than an assumption based on the receiver's original class name.
CHAPTER 12 OF 14 · Transfer and verify
12. Transfer the method to a changed archive review list
The archive has five positions Q10, Q20, Q30, Q40 and Q50. It wants to replace the second position from the end with Z90 in a new list. Resolve negative two against length five to actual index three, then predict Q10, Q20, Q30, Z90 and Q50.
Now make source index three an accessor that would log a read. The selected position is replaced directly in the copying loop, so that old accessor value is not obtained there. Move the selected index to two and the source accessor at three becomes a retained read instead.
Change the requested index to five. The actual index equals length and is rejected before indexed copy reads. A separately evaluated replacement builder may already have run, so record that marker independently from the absent indexed getter markers.
Next place a retained getter at zero that adds a sixth source position during construction. The captured length remains five. The new sixth position is not included merely because it exists by the time the invocation finishes, while a later retained value can still reflect a deliberate change made before its own read.
Finally, put shared record objects at the retained positions. The new archive list is a new container without automatically becoming a new set of independent records. Decide which view receives the returned array and which existing views retain the source reference.
This transfer changes length, relative index, getter placement, range validity and ownership independently. A learner who memorized only that the three-slot result contained X80 cannot explain all these outcomes. The method's stage order and selected read exception must carry the reasoning.
For practical reporting, include the original extent, supplied index, resolved position, replacement value identity, read log, returned array and source state. Those observations make it possible to connect a correct low-level replacement with the application's intended review action while preserving the limits of what the fixture actually established.
CHAPTER 13 OF 14 · Transfer and verify
13. Build checks around bounds and read selection
Start with a passive three-element array and verify returned contents, fixed length, new outer identity and unchanged source. Add negative one, negative length and both immediately invalid boundaries. Assert RangeError rather than an arbitrary fallback result for the rejected calls.
Compare the negative-index method call with ordinary bracket assignment on a fresh source. Inspect both the final indexed element and the negative-looking named property. This distinguishes relative-index interpretation from ordinary key selection.
Use a getter at the replaced source position and assert that it is not invoked by the method's copying loop. Then replace another position and assert that the now-retained getter is read. This catches an implementation that copies all old values before overwriting the chosen result slot.
Add a retained getter that changes a later retained value and another that changes source length. Check captured extent and individually read values separately. Keep an out-of-range fixture with length and index-conversion logs to locate failure before indexed copying.
For sparse and inherited inputs, use local controlled sources and exact result property checks. For object-valued elements, assert which child identities remain shared. If subclasses or generic receivers are supported, assert the returned interface rather than only its displayed values.
Replay the five-position archive example before changing the consuming feature. Then follow the returned array into the chosen current-list update. A correct method call can still be ignored by a caller expecting in-place mutation.
These checks establish the selected language behaviour in the recorded runtime and declared domain. They do not verify browser rendering, remote persistence or authorization. Wider acceptance evidence needs to observe those steps directly, while keeping the same distinction between a replacement request, a returned list and the visitor's completed task.
CHAPTER 14 OF 14 · Transfer and verify
14. Report the resolved position and the values actually read
A useful report starts with source length, supplied index and the resolved actual index. It states whether the range check passed, which source positions were read and which output position used the supplied replacement instead. Then it records the returned container and the relevant source state.
For the opening review panel, the concise explanation is that with created a new three-slot array, replaced index one directly with X80 and retained the other source values. It did not assign X80 into the original array. A getter at that replaced source slot was unnecessary to its copying loop.
For a negative index, show the length-based resolution. For an invalid boundary, identify the RangeError before indexed copy reads. For a retained accessor, record its effect and the later value that effect changed. These are different explanations with different tests.
For independent practice, ask a learner to invent a four-position review list with one getter and a negative replacement index. Require the resolved position, getter log and final source and result predictions. Then move the getter onto the selected replacement slot and revise only the affected read evidence.
The practical repair is a caller and helper that agree about replacement, bounds, source domain and returned product. It should preserve a valid requested change, reject or recover from invalid current positions under a stated policy, and retain the record ownership the feature actually needs.
Once relative indexing, range checking, selected-position substitution and retained reads are separate, Array with becomes predictable. It supports deliberate copied replacement without being mistaken for bracket assignment, appending, an atomic snapshot or a complete saved editing transaction.
Continue through the verified reading route
Separating toSpliced Copies from splice Mutation in Sparse Array Edits
Separating toSpliced Copies from splice Mutation in Sparse Array Edits. A variable-length copied edit has different deletion, insertion and return-product rules; continue there for the fuller sparse-copy comparison.
Separating a Frozen Outer Object from Immutable Nested Records
Separating a Frozen Outer Object from Immutable Nested Records. A new outer list and a protected outer list do not automatically supply independent immutable child records.
Separating parseInt Prefix Parsing from Complete String Number Conversion
Separating parseInt Prefix Parsing from Complete String Number Conversion. The accepted spelling of a numeric field needs its own grammar before conversion or relative-index resolution.
Separating Target Setter Calls from Data Property Creation When Copying Objects
Separating Target Setter Calls from Data Property Creation When Copying Objects. Source reads, new data properties and later shared record state are different stages of copying.
Separating Own Property Presence from Inherited Lookup Results. Check own indexed presence separately from an undefined read when recording sparse source and copied result shapes.
Frequently asked questions
Does with replace the original array element?
It returns a new array containing the replacement. The ordinary source replacement assignment is not performed.
Is a negative method index the same as a negative bracket key?
No. The method resolves relative indices against length. Ordinary bracket access follows property-key rules.
Can an index equal to length append an item?
It is outside the replacement range and causes RangeError under this built-in method.
Is the old value at the selected source index read?
The copying loop uses the supplied replacement there instead. Other caller work can still read it separately.
Does copying mean that source getters cannot have effects?
No. Retained indexed reads, length access and argument conversions have their own observable stages.
Is the returned array an independent deep snapshot?
No. Retained and supplied object values can remain shared references, and active source reads are sequenced rather than globally atomic.
Authoritative sources and further reading
ECMAScript: Array.prototype.with. Relative index resolution, range rejection and selected-position substitution.
ECMAScript: Array.prototype.at. A read operation can return undefined where replacement rejects the position.
MDN: Array.prototype.with. The copied result, generic receiver and sparse input contract.
Return to the Websites subject guide, The Gold Standard of Education, or the How X Works hub.