We can use .length
to get
the number of elements in an array,
also known as the length of the array.
In the example given above:
In the example given above:
-
The sports
array
initially contained 5
elements.
-
Then another element was added
to the index 9
.
As we saw in the previous chapter,
this means that all the indexes
from 5
to 8
will contain
the value undefined
.
-
After that, when we checked
the value of sports.length
,
we got the value 10
because
even the undefined
elements
were counted.