So now let's look at some selected methods of a race and we just picked a few.
There are a lot more.
And if you look at the Mozilla documentation, you'll find out about all of them.
So two of the most popular methods for the arrays are push and pop.
And what push does is pushes the value onto an array and pop or pop the value off the last value.
So if we push 25 on and we pop, we'll get 25 back.
And then there's some that may seem a little familiar from strings.
For example, there's an index of where is the index of a value of 25 for the last index of you're going
to reverse an array to turn it from front to back.
Slice is a lot like the string slice.
So it gets the values starting at position two and then prior to index four.
And then you can do sorting with an array.
And this is very important.
So if you have an array of strings, you can use this method without doing anything else.
So in my earlier example, we have an array of names.
You can sort them alphabetically, but if you have numbers or something more complicated, you need
to provide a function to do the sorting.
And we'll talk about that in a later section.
OK, I'm back in this array program that we started in the previous section, so it's going to come
in kind of handy here.
So let's do a couple of methods.
So if I say push, I will say my array that push 100 and then let's log that out.
And then I'm going to pop that out as well.
So let's just repeat this, except for the very first thing I'm going to put on the log is the pop.
So it's kind of Poppit, it's going to put out there and it's going to turn out the one.
Let's come on a few things out.
All right, so the first thing we do is we pushed 100 onto the array and now it's here at the end and
then we popped it and you can see we got 100 off and now they're raised back to five in length.
And if we keep popping, we're going to pop things off with pop more than we can get.
But you can see we're getting one hundred eight six five four two two is the last thing, right?
I don't think I'd consider it loud enough.
I wanted to do it a couple more times just to see what happens when the array is empty.
So when it's empty, we get undefined.
So I said, OK, it's undefined, it's empty.
Empty array in its length is zero.
So no matter how many times you try to pop into your head, you're not going to get anything out of
it, but you can then push again.
Another problem there, if I take this push and put it down in here, we can push that that 100 back
onto the tray.
And so then we got one hundred off at the end.
All right, let's try something else.
At these out, we can try the index of.
And we know we have an index of, say, number five.
So we can say cancel that log, my array index of and you can see like some of the other ones, it has
a search element and a from index.
Where do you want to start searching?
So we'll just say five won't specify from index.
So we've got the number two, and if we scroll back up here, we can see that the five is a position
to zero one to.
So that's how I found it.
How about the reverse?
We won't worry about the last index except for right now.
So if I say console that, log my array dot reverse.
And so eight, six, five, four, two.
In the era, did it change the era, let's add that to the log.
Yeah, it did.
We have to be careful about which ones are using some of these will return a new array and some of them
will change the array itself, like push changes the array.
So one last one will try is the sort so we can use the sort method, but it's going to need something
to actually make it sort correctly.
So let me demonstrate.
How can so it looks right.
So I'm going to say my array and I'm going to put in some strings and I'll say a.
I start with something like lower
Cafcass square brackets.
And then, as I says, you you a with quotes and we'll leave it there.
So now I say, Meyera, that sort,
and we want to put it up.
So we've got our very nicely sorted ways and then the ends and the has been used and disease.
So we run into problems though is if we have numbers in here, because numbers in the sort of algorithm
are treated like strings.
And so sorting numbers is going to work very well demonstrated.
Versus changes back to a number array.
We'll say one thousand or one hundred thirty ten.
Fifteen hundred, three hundred and thirty three.
Twelve.
OK, so let's see what happens when we start that array.
So now we get an order that we're not expecting because it's sought by the as if it's a string, so
sorting based upon the first character going left to right, so says, OK, the ones come first and
then the threes.
But you see the one zero comes before one too, which comes before one five.
So the numeric value of the number doesn't matter.
It's only the string.
So the sort numbers we have to do something more or in the next two sections we're talking about the
spread operator and de structuring arrays and this is a little more of an advanced topic.
So you could skip and come back to it or you could go through it now and come back to it later.
But it's something you definitely need to practice.