Course contentsShow
← JavaScript
Lesson 803 of 2,72319. ArraysFree lesson

The Length Property

Access the length property to determine how many elements an array contains.

The Length Property

You'll learn how to use the .length property to find out how many elements an array contains.

What Is the Length Property?

Every array in JavaScript automatically has a length property that tells you exactly how many elements are stored in that array. Think of it like a label on a box that always shows the current count of items inside — you don't have to manually count them yourself.

How It Works

The length property uses dot notation to access this special value. If you have an array named colors, you can check how many colors are in it by writing colors.length.

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

The length property always reflects the current state of the array. Since arrays use zero-based indexing, the length is always one more than the highest index:

const numbers = [10, 20, 30, 40];
// Indices: 0, 1, 2, 3
// Length: 4

Why It Matters

Knowing an array's length is essential for many tasks. You'll often need this information to avoid accessing indices that don't exist, or to process every element in the array systematically.

const items = ['pen', 'paper'];
const lastIndex = items.length - 1; // 1 (the last valid index)

An empty array has a length of zero:

const empty = [];
const size = empty.length; // 0

Key Takeaway: The .length property returns the number of elements in an array, helping you understand its size and avoid index-out-of-bounds errors.