In this movie, we're going to take a look at some methods that are provided to work with arrays. Now the array object, or more correctly stated the prototype of the array object has multiple methods that you can access from an array, and they allow you to do different things with an array. We're going to look at some of the simpler methods. There are some pretty complex methods that are available. Those will be addressed in the advanced course on arrays. Let's take a look at which methods we'll be covering.
Now to string has already been been introduced, but that's one of the methods and we'll use that to introduce the next method which is join. Join is very similar to to string but join allows you to specify the character which is used between each element of an array when it creates a string out of it. So to string and join, do much the same thing. The reverse method simply reverses the order of an array. So the last element of an array will become the first element and the first element will become the last sort allows you to place the elements of an array in alphabetical order. Index of and last index of are both methods that are available for searching an array and finding out if a particular value exists in an array.
Index of searches from the beginning have an element and just like index of which which we've used with strings. It returns a number which indicates the position of that value in the array. So if it finds the value at position one, it returns a zero position to returns a one time If it cannot find the value, it returns a negative one. last index of works exactly the same way with one difference, it begins it search from the end of the array. So index of searches from the beginning of an array, last index of searches from the end of an array. Alright, let's open up the console and take a look at some of these different methods that are available.
So first off, I'm going to create an array I'm going to paste it in, so you don't have to see me type all of these different fruit names, but it's simply an array of fruits. And now we're going to use some of these methods to work with this array. So first, let's do the to string method, which we already introduced. Now, as you will remember to string places or creates a string, out of the array and it places a comma between each element of that array. We can see that There. Now the join method, as I said is very similar to the to string.
In fact, if I don't enter anything in the parentheses of join, it comes back with the exact same thing as to string does. But what join allows me to do is specify what characters use to separate the elements. So let me do join again. And this time, I'm going to enter a hyphen as the character, and also places that between each element now we're not limited to a single character. For example, I can enter, double hyphen, and that's what gets put between. So join in to string very similar join just allows you to do a bit more.
All right, let's take a look. Look at reverse. Now notice the order of the fruits from our join, and our two string statements. Now with reverse, it has switched that order, banana, apricot, raspberry Now, don't get confused. I have banana in here twice. There's banana as the first and banana as the last element of this array.
Now notice what reverse did let me type in fruits to display the array. And notice it physically changed the array. So it didn't just reverse the elements for display purposes. It it physically change which order they exist in that array. All right, and that's the same thing that will happen with the sort method sort will do this alphabetically and it will physically change The array. So now if I type fruits again, we can see that it's still in that alphabetical order.
All right. Now let's look at index and last index. That's for searching inside an array to find something. Let's say I'm looking for plum, I want to find out if plum is in the array. Notice it returns a negative one. So it returns a negative one if it cannot find the element in the array.
Otherwise, let's go ahead and do banana. If I would do it right, then we wouldn't get the air it returns a two. So if we look at the fruits array again, notice zero is Apple. One is apricot two has banana is also banana. And notice, when we do last index of a, press the up arrow bring up that statement again, I'm going to change index up to last index of, then we get three because last index up begin searching from the very end of the array and comes backwards. So that's the difference between index and last index up.
So those two methods can be very helpful if we're searching an array for a particular value. All right, those are the methods that allow us to work with earase. Let's continue with the next topic.