Techloghub - Dev Tricks
.sort() mutates the original array.This has probably bitten you at least once.
const nums = [3, 1, 2]nums.sort()console.log(nums) // [1, 2, 3] — original is gone
New immutable methods fix this:
const sorted = nums.toSorted()const reversed = nums.toReversed()const spliced = nums.toSpliced(1, 1)
Original array stays untouched. You get a new array back instead.
✓ toSorted() — non-mutating sort✓ toReversed() — non-mutating reverse✓ toSpliced() — non-mutating splice✓ with() — non-mutating index update
Baseline 2023. All modern browsers + Node 20+.

Comments
No comments yet
Be the first to share your thoughts.