JS Built-ins
← Array145 / 226
ArrayArray.prototype.pushintermediate

Array.prototype.push() β€” build an array with a loop

πŸ“– Usage Example
const result = []
for (let i = 1; i <= 3; i++) result.push(i)
result  // β†’ [1, 2, 3]

Use push() inside a loop to progressively build an array one element at a time.

Building arrays with push

A classic pattern: start with an empty array and push values in a loop.

const result: number[] = []
for (let i = 1; i <= 3; i++) result.push(i)
// result === [1, 2, 3]
πŸ’‘ Hints (3)
  • β€’ Start with `const result: number[] = []`.
  • β€’ Loop from `1` to `n` inclusive with `i <= n`.
  • β€’ Call `result.push(i)` on each iteration.