So you might be wondering, why would you use an array will arrays are very powerful and they give you
a lot of stuff right out of the box, you can add remove items from the array.
You can also splice and there's really a lot of things you can do with arrays.
So there's these what's called as array methods.
This is a quick example of how you can add content to the array.
And there's a lot of different methods that are available within arrays.
So we've got our typical array.
We can find out a length of the array.
So we know that there's two items in there.
So that's we're getting that length.
We can also do index into an item so we can subtract.
So knowing that we've got the length of the array and we could subtract one.
So that will give us the last item that's in the array.
So basically giving us that banana.
So there's some flexibility over returning those items we can loop through so we can iterate through
the arrays.
And there's going to be a little bit more on loops coming up.
There's also you can add items to the end of the array.
So in this case, using the push method.
So referencing the array and then we use both push and then specify the value that we want to add in.
And as you can see, the series has now changed.
It's got apple, banana and orange.
You can use pop to remove out the last item from the end of the array.
There's also a shift.
So that gives you the ability to move an item from the front of the array and then you can add an item
in the front of the array using on shift.
You can also find an index value of an item in the array.
So there is using index of and if there is no value, if nothing, if that item banana is not within
the array, then it's going to return back a negative one.
And this is often used in order to quickly search for the contents of the array.
And you do a condition to check to see if index of banana is equal to negative one.
And if it is, then, you know, it's not found in the array.
You can also remove an item by its index position.
So that's using Splice.
And I know there's quite a few things that were going over.
And I'm going to show you some examples of these.
There's also removing items from an index position.
There's copying arrays.
So I think you get the picture.
There's just a ton of stuff that you can do with arrays, and that's why everybody loves using arrays.
So this lesson challenge, of course, has to do with arrays.
The objective is to use various methods that we've looked at in order to transform the array below from
this to this.
Actually, quite a few possibilities.
And I can walk you through some of the options that we have using the index of push pop shift on shift
splice in order to rebuild this array and change the value.
We can also get things back like the length and of course, try that output into the console and see
how you can make adjustments to the array trying out all these different common methods that are available
for arrays.
And I'll walk you through it so you can post a video.
Try this on your own as well.
I'm going to walk you through some of these methods.
So let's start with our favorite list array.
And there's are items sitting there in the list array.
So it's console we can console log it out or we could just simply look into the console here.
So you see, you've got a length.
You can also see when you press the dot, Chrome is giving us some suggestions of what we can do so
we can do all of these different methods.
So let's try some of these out and we're going to manipulate the array.
Also going to be constantly console logging out the list to see where we're at.
So checking the list.
So let's make some updates to the array so we can check to see if it is an array.
So if it's a valid array, we can do array is array, temp, equal array is array and we'll also output
the value of temp as we refresh it so we can see those values within the console of the browser.
So we see that it is a valid array.
So that's a good thing.
Let's update the array.
So we're taking the list and selecting value with the index of number one.
And let's update that to Hello World so that when we refresh our value for the last time has updated,
we can also return back index values.
So using temp again as are variable and then the list array.
Let's check for an index of and we'll look for the value that we've updated and replaced.
So we see that temp comes back as negative one because it's no longer within that array.
Let's check for Lawrence.
That one has an index value of zero.
Let's check for 35.
That has an index value of negative one because we're checking for a string.
And there's only a number available there, so if we do 35 as a number, you can see that it does get
returned.
So remember that there is a difference between strings and numbers and it will distinguish differentiate
between the difference.
Let's make some more updates to the array.
So adding in push and we'll just call this one pushed.
You can see that it gets added to the end of the array.
And this time we're going to do pop.
So it's removing the last one.
So basically it removed the one that we added.
There's also shift.
So that removes the first element from the array.
And another neat thing is if you want to get the value that's being returned back, if you want to get
the value that's popped, that you can see that we did get this value being returned back.
It will make the update to the array, but then it will also return back that value that it pulled out.
So if you do want to use that within your code, you can always reference that value in a variable and
then make use of it within your code.
There is shift as well.
So it's adding an element to the beginning.
So just call that on shift so that we can see that within the array.
So it's going to add it to the beginning of the array.
So it's added in the zero position.
So it's shifted all the rest of the index values.
And that's why setting the values using this type of format, we're using the index format.
This isn't the best way to go about it, because as we've seen, these array values can be shifting
and changing.
So there's quite a bit of stuff that can happen with arrays.
So they're also probably wondering, well, what happens if we do temp and on shift?
So what this will do is this will return back the length.
So the length is eight and if we had more items in there, so if I added in one more and then you can
see that it's returning back the length of the array.
So as we're updating values, you can see that those are updating the length of the array.
So that's what's being returned back.
So unstaffed will add it to the front of the array.
So it makes it first.
What Splice does is it changes the content delivery, adding new elements while removing old ones.
So this is an interesting one.
So what Slice will do is this.
As we can see, it added an item, but it also removed out items.
So basically we started at index value one and we removed out a couple items from index value number
one.
And if we use temp, you can see what's being returned.
There is the two items that got removed out.
So that's what we can do with Splice.
So our starting index position of where we want to remove it and then how many items we want to remove
out of the array.
So I know this lesson covered quite a bit of stuff and it is important to understand all the different
things that you can do with the race.
And this is really just scratching the surface within the modular developer network.
We saw there is so much power and so many things that you can do with arrays.
So even more is going to be coming up in the next lesson.