Arrays allow you to capture a list of objects of the same type, and find individual objects using a number, called an index.
The index always starts at 0. You can call this the “zeroth element” of the array.
To get a specific element, use [ ] square brackets after the array variable name, with the element number inside. If you use an index that isn’t in the array, you will get undefined
Using .length
will give you the total number of elements in the array.
- If the length of the array is 1, the index of that one element will be
[0]
- If the length of the array is 0, there are no elements in the array. Trying any index will give you
undefined
. - If the length of the array is 7, the index of the last element will be
[6]
- In general, you can find the last element using
[myArrayName.length - 1]
You can read more about arrays here:
- Basics: Arrays
- More detail: TypeScript Arrays