JS Built-ins
← Array127 / 226
ArrayArray.prototype.lengthbeginner

Array.prototype.length β€” read the length

πŸ“– Usage Example
const arr = [10, 20, 30]
arr.length  // β†’ 3

Read the length property to get the number of elements in an array.

Array.prototype.length

Array.prototype.length is a property (not a method) that returns the number of elements in an array. It is always a non-negative integer and updates automatically as elements are added or removed.

Challenge: Implement getLength(arr) that returns the length of the array.

getLength([1, 2, 3]) // β†’ 3
getLength([])        // β†’ 0
πŸ’‘ Hints (2)
  • β€’ `arr.length` is a property β€” no parentheses needed.
  • β€’ The length of an empty array is `0`.