**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Preview

Start a free Courses trial

to watch this video

Learn how to create and work with arrays that contain other arrays, or "multidimensional arrays".

[MUSIC]
0:00

Arrays can contain strings,
numbers, Booleans, and other types.
0:09

Arrays can also hold any
combination of values.
0:14

For example, an array might have
the string Hello in the first position,
0:17

the number 23 in the second, and
the value true in the third position.
0:22

In fact,
you can place an array within an array,
0:26

even create an array that contains
nothing but other arrays.
0:29

An array inside an array is called a
multidimensional or two dimensional array.
0:33

That might sound a bit strange and
confusing right now, but you can start to
0:38

think of a multidimensional array
as a list containing other lists.
0:43

You can picture a two-dimensional
array as a spreadsheet.
0:46

Think of the spreadsheet as the master
list, a list containing other lists.
0:50

The data in the spreadsheet is
organized into rows and columns.
0:55

Each row represents one array or
one of the arrays inside the master array.
0:59

And each column cell represents
one element in an array.
1:04

For example, you might use an array to
store the grades of multiple students.
1:08

Let's say three students took four tests.
1:13

Each row represents one student,
and each column is one test score.
1:16

For instance,
the first row represents Student 1, and
1:21

their test scores are 80, 90, 100, and 95.
1:25

The second student's scores
appear in the second row, and
1:29

their scores are 75, 95, 85, and 100.
1:33

The third student's test
scores are in the last row.
1:37

You've probably seen data
like this in a spreadsheet.
1:40

So what would this look like as
a multidimensional array in JavaScript?
1:43

You start by creating an array,
then at elements inside the array,
1:47

each element is another array.
1:51

You'd add the first
student's grades like this.
1:54

Notice that there's another
set of square brackets,
1:57

which indicates that there's
an array nested inside the array.
2:00

You add the next set of student grades
by adding a comma and another array.
2:03

Another comma and another nested
array adds the last student's grades.
2:08

Now, how do you access these
different sets of grades?
2:12

First, let's start with how you get
the grades for an individual student.
2:15

Since each set of student grades is
one element in the grades array,
2:20

you use the index notation
you learned about earlier.
2:25

For example, the first student's
grades are at grades[0],
2:28

the first element in the grades array.
2:31

The second student's grades are in
the second element, at index position 1.
2:34

And the third student's
grades are at grades[2].
2:38

Because each of those index
values represents another array,
2:42

you can access an element in a nested
array using another index value.
2:46

For example,
2:51

the first array of student grades is that
index position 0 of the outer array.
2:51

To access the last grade in that
first nested array, chain a second
2:57

set of square brackets holding the index
value of the last element, like this.
3:02

Now, how would you get to the first
grade of the last student?
3:07

Well, that grade is in the last
nested array, which is at index 2 and
3:10

position 0 of that array.
3:14

This example uses arrays of numbers
to represent student grades.
3:17

But you can create a multidimensional
array containing lists of any value,
3:21

strings, numbers and
Booleans, for instance,
3:25

You need to sign up for Treehouse in order to download course files.

Sign up