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

Array.prototype.concat() β€” merge three arrays

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

concat() appends array elements at one level β€” nested arrays are NOT flattened.

Array.prototype.concat() β€” chaining multiple arrays

You can pass multiple arrays to a single concat() call. This merges them all without intermediate arrays.

Challenge: Implement mergeThree(a, b, c) that returns a single array containing all elements from a, b, and c using a.concat(b, c).

mergeThree([1], [2, 3], [4]) // β†’ [1, 2, 3, 4]
mergeThree([], ['x'], ['y']) // β†’ ['x', 'y']
πŸ’‘ Hints (2)
  • β€’ `a.concat(b, c)` is equivalent to `a.concat(b).concat(c)` but more efficient.
  • β€’ All arguments to `concat` are merged in order from left to right.