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

Array.prototype.concat() β€” append a value

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

Use concat() to append individual values to an array, returning a new array.

Array.prototype.concat() β€” with a scalar value

concat() also accepts non-array values and simply appends them as elements.

Challenge: Implement appendValue(arr, val) that returns a new array with val appended, using .concat(val).

appendValue([1, 2, 3], 4) // β†’ [1, 2, 3, 4]
appendValue([], 'hello')  // β†’ ['hello']
πŸ’‘ Hints (2)
  • β€’ `arr.concat(val)` works when `val` is not an array β€” it gets appended as an element.
  • β€’ When `val` is an array, concat spreads it. Use `arr.concat([val])` to force it to be treated as a single item.