JS Built-ins
← Array131 / 226
ArrayArray.prototype.lengthintermediate

Array.prototype.length β€” length after pushing elements

πŸ“– Usage Example
const arr = []
arr.push('a')  // returns 1
arr.push('b')  // returns 2
arr.length     // β†’ 2

length updates automatically β€” check it after mutations to know the current array size.

Array.prototype.length β€” after push

Every time you push an element onto an array, length increases by the number of elements pushed. Knowing the resulting length without having to recount is useful in tight loops.

Challenge: Implement lengthAfterPush(arr, ...vals) that pushes all vals into arr and returns the new arr.length.

lengthAfterPush([1, 2], 3, 4) // β†’ 4
lengthAfterPush([], 10)        // β†’ 1
πŸ’‘ Hints (2)
  • β€’ `arr.push(...vals)` spreads all rest arguments into the push call.
  • β€’ `push` itself returns the new length β€” you could also return that directly.