Okay, we've already talked about variables.
In this video, I wanna talk about arrays.
And arrays are just incredibly important to any programming language.
And they're really sort of the beginning of more intermediate programming stuff.
We've been talking about basic stuff up until now.
Arrays, we're starting to get into more advanced stuff.
They're just one of those fundamental things that all programming languages
have, and you're gonna use them a lot.
So if a variable is a container, or
a bucket, that holds one thing, an array is a container that holds lots of things.
In fact, they'll hold as many things as you can cram in there.
And you can keep putting things in, and you can take things out.
And you can do all kinds of things.
So they're great for
storing lists of things like lists of names or customer phone numbers or
email addresses or anything you want, pizza toppings, it doesn't really matter.
So, how do we make an array in JavaScript?
Well, it's pretty easy, and it looks a lot like a variable.
So if we come up here, we can go, var, we're gonna declare it, give it a name.
Let's call it pizza and equals to.
Up until now it looks just like a variable.
Now we use these brackets.
And every item we wanna put into the array we just put quotations around.
So we could go "pepperoni" and then we separate each item with a comma.
So "pepperoni", "hamburger", "cheese", "mushroom", whatever.
That's an array, very simple.
So you can see I've got all this on one line, and
in fact you can put this on multiple lines, and that's sort of the convention.
Just because it makes it much easier to read.
This is a very small array, if you have a huge array with hundreds of things in it,
or something you wanna put it on separate lines,
just because it's gonna be easier to read.
So, we can call out things from our array, and we can and do things with them,
just like when we output variables to the screen we can do the same thing.
And we do that by referencing the index number of each item.
Each item in an array has its own index number.
And the numbers start at zero.
This is very important.
This screws up a lot of people.
So "Pepperoni," this is the zeroth item.
Right, it's not the first item, it's 0.
Hamburger is number 1, cheese is number 2, mushroom is number 3.
So there're 4 items in here, but it only goes up to 3 in index numbers.
That confused a lot of people.
Even to this day I'll forget every once in a while, and I'll wanna use pepperoni and
I'll call for variable index number one, and it'll be hamburger, and
I'll be like, yeah, I forgot, it's zero.
Try and keep that in mind.
So, to access an array item, all we have to do is just call our array,
do this, and boom, 0. That's it.
So if we wanted to go document.write, just like we have in the past for everything.
So we're calling out our array we wanna output the zeroeth item.
That's pepperoni.
If we wanna output the third item, that's mushroom.
We can also reference the entire array by not giving it an index number,
just calling it pizza.
If we do that, we're gonna get everything output separated by a comma.
Now we an loop through these and pull out each item automatically.
We're gonna talk about loops later.
And when we do, we'll look at arrays and loops and you'll see how to do that.
So you can put all kinds of things in arrays.
We've got strings here.
You can also put, we can put numbers.
That works just as well.
We can put other arrays in an array,
that will blow your mind later when we start to do that.
We can do math with arrays, so 41 and 2.
Now if we come down here and call pizza, let's see this is 0, 1,
2, 3, this is the 4th and the 5th item.
So if we go 4 + pizza 5,
Typo, 04 + 05.
That's gonna be 43.
So we can do addition,
we can do anything we want with an array that we can do with a variable, basically.
We can ask it what the length is,
how many items are in there by calling length, pizza.length.
And we can see there are six items in our array.
We can do sort which is kinda fun.
So we got pepperoni, hamburger, cheese.
If we sort them, it does it, oops, actually we need to sort and
then two little parenthesis.
Now it puts it in alphabetical order, now it's cheese, hamburger, mushrooms and
starts with 2 and 41 'cause 2 is smaller than 41 so that's automatically sorting for us.
We can add things to our array.
We can go pizza.push to push something in.
And then we can go, say, "onion", right.
Now, if we call our array,
we can see "onion" has been added to it automatically, programmatically.
So, those are arrays, they're really fun and
in the next video we'll talk about objects.