splice() is a JavaScript array method that removes, replaces, or inserts elements at a chosen position. It changes the original array in place and returns the removed elements, so developers use it when they need direct array editing rather than a copied result.
How splice() edits arrays in place
splice() is a mutation-oriented array method, so its defining trait is that it changes the original array rather than creating a copy. That makes it useful when the calling code intentionally wants to alter ordering, remove elements, or insert new values at a specific index and keep those changes visible to later logic.
The method accepts a starting position and then, optionally, a delete count and replacement items. Because the same array object is updated, its effect is immediate and shared by any code holding a reference to that array, which is an important distinction from non-mutating methods such as slice() or concat().
Removal, replacement, and insertion behaviour
splice() can perform three related operations: it can remove elements, replace them, or insert new items without removing anything. In practice, that means it can be used to trim a list, swap out a segment, or inject content into the middle of a collection without rebuilding the entire array.
The return value is also part of its design. splice() returns the removed elements as a new array, which lets developers both change the source array and still inspect or reuse the deleted items if needed.
Indexing, ordering, and side effects
Because splice() works by index, its behaviour depends on the array's current order at the moment it runs. That is powerful for direct editing, but it also means off-by-one mistakes, unexpected ordering changes, or stale assumptions about length can produce subtle bugs.
Its in-place nature is the key side effect to remember. If multiple parts of an application share the same array reference, a single splice() call can change state for every consumer of that array, which is sometimes exactly what is wanted and sometimes the source of hard-to-trace defects.
When splice() is the right tool
splice() is the right choice when the task is fundamentally about editing an existing list rather than deriving a new one. That makes it a natural fit for UI state updates, reordering items, and data cleanup steps where mutation is acceptable and explicit control over the original array matters.
It is less suitable when immutability is important, such as in workflows that rely on predictable state history or referential comparison. In those cases, a non-mutating approach is usually easier to reason about, even if it requires a little more code.
Related resources from NHI Mgmt Group
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on October 29, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org