Search⌘ K
AI Features

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.

Node.js
let days = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' ];

Arrays do not have to contain elements of the same type:

Node.js
let storage = [ 1, 'Monday', null ];

Accessing elements

Each element of the array can be accessed using an index starting from zero. The syntax for indexing is shown below:

Node.js
let days=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
console.log(days[0]);
console.log(days[4]);
console.log(days[5]);

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:

Node.js
let days=['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
console.log(days.length);
Technical Quiz
1.

What notation is used to access the 10th10^{th} element of the array arr?

A.

arr.(9);

B.

arr(9);

C.

arr[9];


1 / 1