Immutable Variants: toSorted, toReversed, toSpliced, with in JavaScript

From the JavaScript Array Methods cheat sheet · Modern Array Methods (ES2023+) · verified Jul 2026

Immutable Variants: toSorted, toReversed, toSpliced, with

Non-mutating versions of sort/reverse/splice and indexed update

javascript
const nums = [3, 1, 2]

const sorted = nums.toSorted()        // [1, 2, 3]   — nums untouched
const reversed = nums.toReversed()    // [2, 1, 3]   — nums untouched
const removed = nums.toSpliced(0, 1)  // [1, 2]      — nums untouched
const updated = nums.with(0, 99)      // [99, 1, 2]  — nums untouched

console.log(nums)                     // [3, 1, 2]   — original preserved
💡 Same shape as sort/reverse/splice — just prefixed with `to` (and `with` for indexed set)
📌 Always returns a new array — safe for React/Redux state, signals, etc.
⚡ Replaces the [...arr].sort() / structuredClone copy-then-mutate idiom
🎯 `with(-1, x)` is the cleanest way to "replace the last element"
es2023immutablemodern

More JavaScript tasks

Back to the full JavaScript Array Methods cheat sheet