-
You already learned about using variables
to store numbers or strings.
-
Now we're going to learn
about something called arrays,
-
which let us store multiple items
in just one variable.
-
As you'll see, arrays let us do
all kinds of useful things.
-
Okay, let's review variables. All right?
-
So we have, var myFriend = "Sophia".
-
So, a variable is just a way
of storing a value, like Sofia,
-
and giving it a label so our program
has an easy way to refer to it later.
-
We can think of it like a drawer,
-
with a myFriend label on the outside
and Sofia on the inside.
-
So whenever we look inside it,
we find Sofia.
-
Now, sometimes we want to hold
multiple values in a single variable
-
and we can't just do it like this
because we'll get a syntax error
-
and our program will freak out,
and all that stuff.
-
So we have a specific way
that we do that,
-
and that's using arrays.
-
So we could say, myFriends = ,
-
and then we have to do
a square bracket,
-
and then inside,
that's where we can put all the values.
-
So we say, Sofia,
and we have John, and we have Leif.
-
All right, so those are my three friends.
-
Maybe in order of how much I like them,
but don't tell them that.
-
Okay, so now our variable
holds three values, not just one.
-
And we can imagine
it's like a chest of drawers,
-
and we put this label, myFriends,
on the whole chest.
-
And we can just open the right drawer
-
to find the value
we're looking for, right?
-
And you imagine, with a chest of drawers,
-
if you want to know
what's inside the first drawer,
-
you would just open it up and look inside.
-
So how do we do that with our array?
-
Well, we can just type
the name of the array,
-
and then the brackets again--
-
ooh, I misspelled it--
-
and then the number
of whatever it is in the array, right?
-
So maybe it would be 1. Okay?
-
So, let's actually try this out
-
by using the text command
and showing Sofia on the canvas.
-
So we say, myFriends--
ooh, friend is a hard word to spell, huh?
-
So, myFriends[1],
and then we put it here.
-
Oh, and then let's put a little fill.
-
Oh, okay, so we see John.
Why do we see John?
-
We said 1 for the element index, right?
-
The 1 right here.
-
Well, that's because
arrays start at 0, not 1,
-
and it'll definitely seem weird at first,
but you'll get used to it.
-
So if we put 0,
then we see Sofia, all right?
-
And then, if we want to show
the next element,
-
then we use 1, all right?
-
So, and then if we want to do
the final element,
-
the third element, then we use 2.
-
So you just think to yourself:
"Okay, which one do I want to retrieve?"--
-
Oh, let's spread these out--
-
and where is it located,
and you just subtract one.
-
So the first one is 0,
the second one is 1,
-
the third one is 2, et cetera, et cetera.
-
What happens if I forgot,
and I try to access Leif this way?
-
Well then, we say myFriends[3],
and we'll just get nothing.
-
That's because there's nothing there, right?
-
When it says 3,
it looks for the fourth element,
-
and there's no fourth element,
so there's just nothing.
-
And that's something that can happen a lot
when you're using arrays,
-
so just look out for that.
-
And the same thing
if I tried to access a hundred
-
because I don't have a hundred friends,
I only have three,
-
so then we get nothing, all right?
So let's get rid of those.
-
Now, let's say we want to keep track
of how many friends we have
-
because I'm really proud,
and I have three friends,
-
and I want to let everybody know.
-
So I'm going to go
and declare this to the world.
-
So, "I have " + numFriends + " friends!!!".
-
Woo, all right.
-
Okay, so I have three friends. Yay!
Oh, that's not very many.
-
Okay, so maybe Winston feels bad for me
and says he'll be my friend,
-
and he says I can add him to the array.
-
And I was like:
"Okay, cool. Thanks, Winston."
-
So I add Winston.
-
Oh, but it still says
I have three friends, right,
-
because I have to go
and update this variable here.
-
That means, every time
that I add something to this array,
-
I have to update this variable,
and that could get really annoying,
-
especially if all of you guys
watching this
-
decide you'll be my friend.
-
And then, you know,
I'm updating this thousands of times
-
and having to update this every time.
-
So here's the thing:
-
We, so often, want to know
how long our array is,
-
that there's a special way to do that.
-
So the array will keep track
of how long it is
-
using a property called length.
-
And to use it, we just say,
myFriends.length,
-
and then we'll get back the length.
-
See, now it says 4,
and I can delete this variable.
-
I don't need it any more.
-
And this property will update
whenever we add.
-
So maybe OhNoes Guy!!
says he'll be my friend, and I'm like:
-
"Okay, you're kind of mean,
but okay, you'll be my friend."
-
And we can keep adding
and it'll keep updating.
-
So, that's really cool because, you know,
-
it's a lot less work to keep track
of how long our array is.
-
All right, so pretty much,
-
whenever you want to store
a list of values like this,
-
we'll use an array.
-
So keep watching to find out
all the really cool things
-
that we can use them for.