Introduction to Arrays
Explore the fundamentals of arrays in JavaScript, learning how to access individual elements by index and how array length works. This lesson helps you grasp the basic structure and usage of arrays to organize data efficiently.
We'll cover the following...
Introduction
An array is an ordered list of items. The items may be of any type. You know, in most post offices, there are hundreds or thousands of post boxes. Each post box may or may not contain something. Each post box has a numeric handle. Post box 25 maybe your box. You unlock it, grab its handle, and access its contents.
The trick is that in the case of arrays, you have to imagine the post boxes with keys 0, 1, 2, and so on. Typically, arrays have continuous keys.
Arrays do not have to contain elements of the same type:
Accessing elements
Each element of the array can be accessed using an index starting from zero. The syntax for indexing is shown below:
In the third example, we indexed out of the array. As we indexed beyond the length of the array, the program returned undefined for days[5].
Array length
Arrays have lengths:
What notation is used to access the element of the array arr?
arr.(9);
arr(9);
arr[9];