JS Built-ins
← Array57 / 226
ArrayArray.prototype.fillbeginner

Array.prototype.fill() β€” fill entire array

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

Use fill() without indices to replace every element with a single value.

Array.prototype.fill()

Array.prototype.fill(value, start?, end?) fills all elements from start to end (exclusive) with a static value. Without start/end, it fills the entire array. The method mutates and returns the original array.

Challenge: Implement fillAll(arr, val) that fills the entire array with val using .fill(val).

fillAll([1, 2, 3], 0)   // β†’ [0, 0, 0]
fillAll([1, 2, 3], 'x') // β†’ ['x', 'x', 'x']
πŸ’‘ Hints (2)
  • β€’ `fill()` mutates the original array and returns it β€” it does not create a copy.
  • β€’ To avoid mutation, call `fill()` on `[...arr]` or `arr.slice()`.