This course has been updated! We now recommend you take the Complete Intro to Web Development, v2 course.

Check out a free preview of the full Introduction to Web Development course:
The "Exercise 7: Manipulating Arrays" Lesson is part of the full, Introduction to Web Development course featured in this preview video. Here's what you'd learn in this lesson:

Arrays can be modified in a lot of different ways. Items can be added at the beginning or end of an array. Items can also be edited individually. In this exercise, you will use a number of these methods to alter an array.

Get Unlimited Access Now

Transcript from the "Exercise 7: Manipulating Arrays" Lesson

>> [MUSIC]

>> Nina Zakharenko: So there are a few ways that we can change or add items in an array. The first way is that we can reference them by number and just update the value and that will override it. The second way to do it is to use this method called push that's available on any array.

[00:00:28] If we push a new value, it'll just add it on to the end. And running this will return the number, the length of the new list. There's also a function called pop, which will pop off the last value in that list and return that. So let's do some exercises.

[00:00:55] So we have our fruits array here. If I wanted to change the first item,
>> Nina Zakharenko: To, let's say Apricot,
>> Nina Zakharenko: I would access this array by index.
>> Nina Zakharenko: And then if I look at my fruits array again, we'll see that the first item has changed to Apricot. To add on a new item to the end of the list, we'll do fruits.push.

>> Nina Zakharenko: Add a new fruit on there. The value that's returned is the length of our new array, which has four values in it. If we want to start removing elements from the end of the list, we'll do fruits.pop.
>> Nina Zakharenko: This function takes no values, so we can just go ahead and do that until our list is empty.

>> Speaker 2: Is there a way of adding something to the head of the list or does it always have to go to the unshift?
>> Nina Zakharenko: What's that?
>> Speaker 2: Unshift.
>> Nina Zakharenko: Unshift. So, would you just do this?
>> Speaker 2: Mm-hm.
>> Nina Zakharenko: So, let's just add some more values on there.
>> Nina Zakharenko: So this is our current state of the array and let's try unshift again with a different fruit.

>> Nina Zakharenko: So if we look at fruits now, we'll see that Nectarine has been added to the beginning of the list.