The array length property tells you how many items are in the array. It helps you know the size of the list you are working with.
Array length property in Javascript
const array = ['item1', 'item2', 'item3']; const size = array.length;
The length property is always one more than the highest index because arrays start at 0.
You can read length to get the number of items, or set it to change the array size.
const fruits = ['apple', 'banana', 'cherry']; console.log(fruits.length);
const emptyArray = []; console.log(emptyArray.length);
const oneItem = ['only']; console.log(oneItem.length);
const numbers = [10, 20, 30, 40]; numbers.length = 2; console.log(numbers);
This program shows how to get the length of an array, use it to loop through all items, and then change the length to remove items.
const colors = ['red', 'green', 'blue', 'yellow']; console.log('Original array:', colors); console.log('Length:', colors.length); // Loop through the array using length for (let index = 0; index < colors.length; index++) { console.log(`Color at index ${index}:`, colors[index]); } // Change length to remove last two colors colors.length = 2; console.log('Array after changing length:', colors); console.log('New length:', colors.length);
Accessing length is very fast, O(1) time complexity.
Changing length to a smaller number removes items from the end, which frees memory.
Common mistake: expecting length to count only non-empty or non-undefined items, but it counts all indexes.
Use length when you need to know or control the size of the array quickly.
The length property tells how many items are in an array.
You can read it to count items or set it to change the array size.
It is useful for loops, checks, and managing array size.