Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A JavaScript array is a list-like object. You can use arrays to store any kind of data in a sequence, like a list.
-
0:00
[MUSIC]
-
0:04
Arrays are one of the most useful data types you have as a JavaScript developer.
-
0:10
Arrays are the best way to group similar data together in a sequence,
-
0:14
such as a list of customers, photos, dates, or
-
0:18
even coordinates indicating a precise spot on a screen.
-
0:22
To use arrays effectively,
-
0:23
you need a way of looking through them to see the data they hold.
-
0:27
This action of looking through arrays is called iteration, and
-
0:31
JavaScript provides many methods for iterating over an array.
-
0:35
For example, you might wanna transform an array's elements, say,
-
0:39
from strings to numbers.
-
0:41
Or you might want to add an array of numbers and return the sum.
-
0:45
Maybe you have an array of dates and
-
0:47
you want to remove all dates before, say, July 1st.
-
0:51
In this course,
-
0:52
I'll show you how to use array methods to accomplish these kinds of tasks.
-
0:57
You'll also practice iterating over arrays so
-
0:59
you can retain the knowledge you'll build here.
-
1:03
Before diving in though, let's take a moment to review what arrays are.
-
1:08
An array is a sequenced selection of data, basically a list of information.
-
1:13
In fact, arrays are sometimes referred to as list like objects in JavaScript.
-
1:19
Think of a list on a piece of paper, like a shopping list for example.
-
1:23
A shopping list is a group of items you need to buy.
-
1:27
Those items appear in specific order, starting at the top and going down.
-
1:32
So when you want to add something to the list, you write it at the bottom.
-
1:37
JavaScript arrays have similar characteristics.
-
1:40
You can store and position data inside them as you can with a list.
-
1:44
To add data to an array, you typically write it in using a push method for
-
1:49
example.
-
1:51
Storing anything whether it is a name on a list or
-
1:54
an item in an array is useless unless you can find it again later.
-
1:58
To find a list item, you'll typically start reading at the top and
-
2:02
go down till you see what you're after.
-
2:05
You can look through arrays in a similar manner, but with arrays,
-
2:09
this process is called iteration.
-
2:11
And unlike a shopping list,
-
2:13
which is really a collection of strings, the names of each item you want to buy,
-
2:18
you can store any JavaScript data type as an item in an array.
-
2:22
A string, a number, an object, and even another array.
-
2:27
JavaScript uses integers called indices to identify the position
-
2:31
of an item in an array.
-
2:33
The first item in an array is that index 0, the next item is at 1 and so on.
-
2:40
This means if you know the location of an item in an array,
-
2:43
you can access it directly with its index.
-
2:46
Put the index and
-
2:47
square brackets after the array to access a specific piece of data.
-
2:52
In the next video, we'll compare two way to iterate over an array.
-
2:57
A for loop, and an array method called for each, I'll see you there.
You need to sign up for Treehouse in order to download course files.
Sign up