We've seen how arrays hold collections of items, and how we can add (.push()
) or remove (.pop()
) items.
But how can we easily find out exactly how many items are currently inside an array?
Engagement Message
Any ideas?
Every array has a length, which is simply the count of items it contains.
An array like [10, 20, 30]
has a length of three. An empty array []
has a length of zero.
Engagement Message
Does the length change when we push or pop items?
JavaScript arrays have a built-in property called .length
to get the number of items. Unlike a method, it's a property that you access directly without parentheses ()
.
You use it like this: yourArray.length
.
What kind of value do you expect .length
to give back?
Engagement Message
Do you think it'll be a string, a number, or something else?
That's right! The .length
property returns a number representing the count of items.
