Length
The length of an array is the total number of elements within the array. A blank array has a length of 0. An array with an element at the 0 index has a length of 1. The length is always 1 higher than the last index number. Array.length
is a built-in property of the array that returns the length.
Knowing the length of an array is critical for iterating over it for actions such as printing the contents, checking for blank indexes, and knowing when to stop looping through it. More on this will be covered in the next module.
File Location
We will be working in the following file:
Practice
Create an array and populate it. Print the length to the console.
Use the methods discussed in the previous module to add and remove items from the array. Print the length of the array to the console each time.
BONUS CHALLENGE
Try to loop over the array and print each element to the console. HINT: Try a for
loop!
Last updated