JS Built-ins
← Array37 / 226
ArrayArray.prototype.concatbeginner

Array.prototype.concat() β€” join two arrays

πŸ“– Usage Example
const a = [1, 2]
const b = [3, 4]
a.concat(b)  // β†’ [1, 2, 3, 4]

Use concat() to merge two arrays into a new one without mutating either original.

Array.prototype.concat()

Array.prototype.concat() returns a new array that contains the elements of the original array followed by the elements of the provided arrays or values. The original arrays are not modified.

Challenge: Implement joinArrays(a, b) that returns a new array with all elements of a followed by all elements of b using .concat().

joinArrays([1, 2], [3, 4]) // β†’ [1, 2, 3, 4]
joinArrays([], [1])        // β†’ [1]
πŸ’‘ Hints (2)
  • β€’ `a.concat(b)` creates a new array β€” it never modifies `a` or `b`.
  • β€’ You can also pass multiple arguments: `a.concat(b, c)` to concat more than two arrays.