JS Built-ins
ArrayArrayintermediate

Array() β€” create an array of zeros

πŸ“– Usage Example
const zeros = new Array(5).fill(0)
zeros  // β†’ [0, 0, 0, 0, 0]

Combine new Array(n) with fill() to create an array of n identical values.

Array constructor + fill()

A common pattern is to combine new Array(n) with .fill(value) to create an array of n identical values without a loop. The sparse array produced by new Array(n) is immediately filled with the provided value.

Challenge: Implement createZeros that returns an array of n zeros.

createZeros(4) // β†’ [0, 0, 0, 0]
createZeros(0) // β†’ []
πŸ’‘ Hints (2)
  • β€’ `new Array(n)` creates an empty-slot array; `.fill(0)` replaces every slot with `0`.
  • β€’ This pattern avoids a for-loop and is very common in algorithm problems.