How to get the length of an array in JavaScript

Getting the length of arrays is fundamental for loops, validation, conditional logic, and displaying counts in user interfaces across all JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve used array length checks extensively in components like pagination controls, progress indicators, and data validation systems where knowing the exact count is crucial. From my extensive expertise, the built-in length property is the standard and most efficient way to get the number of elements in an array. This property is automatically maintained by JavaScript and provides instant access to the array size.

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

const fruits = ['apple', 'banana', 'orange']
const count = fruits.length
// Result: 3

The length property returns the number of elements in the array as an integer. In this example, fruits.length returns 3 because there are three elements in the array. This property is automatically updated whenever elements are added or removed from the array. For empty arrays, length returns 0. The length property is read-only for most practical purposes, though it can be set to truncate or extend arrays (setting it to a smaller value removes elements, setting it larger creates empty slots).

Best Practice Note:

This is the same approach we use in CoreUI components for pagination calculations and conditional rendering based on data availability. Be aware that sparse arrays (arrays with gaps) still count empty slots in their length. For counting only defined elements, use array.filter(item => item !== undefined).length. The length property is the fastest and most reliable way to get array size.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.