JS Built-ins
← Array40 / 226
ArrayArray.prototype.concatintermediate

Array.prototype.concat() β€” immutability

πŸ“– Usage Example
const original = [1, 2, 3]
const extended = original.concat([4, 5])
// original β†’ [1, 2, 3]  (unchanged)
// extended β†’ [1, 2, 3, 4, 5]

concat() never mutates the original array β€” it always returns a new one.

Array.prototype.concat() β€” does not mutate

concat() always returns a new array. The original array is never modified. This makes it useful in functional programming and state management patterns.

Challenge: Implement safeConcat(arr, extra) that returns a new array combining arr and extra, without changing arr.

const original = [1, 2, 3]
safeConcat(original, [4, 5]) // β†’ [1, 2, 3, 4, 5]
// original is still [1, 2, 3]
πŸ’‘ Hints (2)
  • β€’ `concat()` always returns a brand new array, leaving the original intact.
  • β€’ This is one reason `concat` is preferred over `push` in immutable patterns like Redux reducers.