Push, Pop, Shift and Unshift
The most common way of working with arrays is by adding and removing elements in the start or end of the array in this video we will explore the most common ways using the push,pop,shift and unshift methods.
Video Transcript
00:00All right, now that we've got that out of our way,
00:04let's just look at a couple more stuff
00:05regarding to arrays before we jump into a live example of it.
00:11All right, so now for example, what
00:12if we wanted to add more things into the array,
00:14but we don't want to delete anything?
00:16What if we wanted to add Danny, but we
00:18didn't want to override Ben?
00:21To do that, let's just do it.
00:23So first of all, I could just push things inside.
00:25By pushing a value into an array, what I'm basically
00:29saying is I want to add more things into the queue.
00:31So I'm going to add Danny.
00:33And by the way, let's just note here,
00:35although I'm just saying generically
00:37that I want to add something, but there's
00:39an important in order, right?
00:40Is Danny now zero or is he three?
00:43Let's take a peek at what comes out here.
00:46So when we push something, that's
00:51one of the names that always kind of confuse me,
00:53and a lot of people get confused by it.
00:55Pushy would think it would push everything down the line,
00:58and Danny would be first.
00:59But no, ladies and gentlemen, push actually
01:03means put at the last position.
01:04Ideally, I would pick a different name,
01:06but that's the convention.
01:09And maybe it's because English is not
01:11my original, original language, so maybe that's
01:13why push doesn't make sense to me.
01:14Maybe the geeks above, it does.
01:18But beyond push, there's a few other ways of doing so.
01:21We could unshift Oso.
01:23And although unshift for me sounds completely moronic,
01:27to be honest, it doesn't make any sense at all.
01:29But that's how we go.
01:30So I'm just going to give her a name.
01:32We're going to call this Bill in honor of the president of past.
01:39And if I refresh my screen again,
01:42we'll see that Bill, he was unshifted to the first position.
01:46I don't know, for me, I would call it shift, right?
01:48Pushed everything aside and put Bill inside.
01:51But it doesn't work that way, all right?
01:53So you just have to remember it and kind of notice
01:56that when you want to add something to the end,
01:58you use a push.
01:58You don't have to even memorize it.
02:00Just make sure that when you're doing it,
02:02you know what you're doing.
02:03So just check and double check.
02:04Look at a pamphlet, write to yourself on a piece of paper
02:08what is pushed, what is unshifted,
02:09so you could easily access it until you memorize it.
02:12Don't feel bad if you don't.
02:14I never really did.
02:14And I've been doing this for years upon years,
02:16so sometimes I just forget and I look it up quickly.
02:19Or I just try it out and then see which one it was.
02:21So those are the two different ways
02:23where we would access things.
02:24If we wanted to do it in an organized way,
02:27you would say why would you want to do it?
02:29If you wanted to move everything one position aside,
02:32obviously unshift is really, really convenient.
02:35If you wanted to add something to the end
02:36but you didn't really want to figure out
02:38how many items you have,
02:40it just automatically adds it to you in the end position.
02:42And by the way, take an exercise to yourself.
02:45Try to create the push function.
02:46Try to create the unshift function.
02:48Do that when we're done with this course.
02:49Because that could be a really cool exercise
02:51just to see that you tied all the information in
02:53and you get it.
02:55All right, what I want to do is one more.
02:57So we, and obviously we're running through a raise.
03:00Like there's no, there's no tomorrow.
03:02And really the goal is cause arrays are,
03:04I wouldn't say that they're boring.
03:05They're actually really great,
03:07but on their own when they stand alone
03:08might be a little bit intimidating.
03:10So we're just running through them quickly.
03:13Understandably, you'll probably learn a lot more
03:15as you start developing and using them.
03:18But for the sake of clarity and the sake of,
03:20of pushing along and getting you to do
03:23the more interesting stuff, we're pushing along.
03:26But the good news is if you're a member on Zero2Geek,
03:29on our website there's over an hour and a half
03:31that goes, delves deeply, deeply into arrays
03:34and gets you to know everything you need to know
03:35about arrays.
03:37But this is definitely all you really need to know
03:39to start running and using it.
03:41But there's a lot more to learn.
03:42And if you want to learn more,
03:43please I do encourage you to check out our membership.
03:46It's really affordable.
03:49And I love having more members on our site, obviously.
03:54And I want to move on to the next topic.
03:56Next topic is what if we want to remove stuff from the array?
03:59Removing stuff from the array is very similar
04:01to adding things into array.
04:03And the way it's done is very simply.
04:05When we use the, instead of push,
04:07we're going to use, and instead of unshift,
04:09we're going to use the shift,
04:10which shift will do the exact opposite of unshift.
04:13So shift is basically going to pull out one element.
04:17So in our scenario, I'm not even going to save it.
04:19I'm just going to shift one out
04:20and run our element again.
04:21So we should have had five.
04:23And our bill should have been our first one.
04:25But now that we did the shift, bill is gone.
04:28Now, it actually sends back to us what it added in.
04:31So I could put here the shifted.
04:36All right, so I could take the shifted
04:38and you know what, we'll take our shifted
04:40and we're going to push it to the end.
04:44All right, so what I did as switcheroo,
04:46I took the thing that was last first, made it last.
04:49This is so useful.
04:50Imagine like iTunes or whatever music player
04:54that you like playing with
04:55and you want to create a music player
04:57that loops through music
04:59and you have a track of 10 songs
05:01and you want to be an endless loop.
05:04What would you do?
05:05You would put all the songs into an array.
05:07What would you do next?
05:08When a song is done, what will you do that song?
05:10You would pull it out, right?
05:11You would do a shift out of that array
05:13and then you would push it to be last
05:14because you want it to go up again, right?
05:16And that's how you create an endless loop.
05:18You create, and there's a lot of other usages of it.
05:20So these are small stuff and quick stuff that we're doing.
05:23I know that it's hard to see the dots connecting right now,
05:27but you will.
05:28You will over time.
05:29And there's so much things that you could do with arrays.
05:31It's an immensely powerful thing.
05:33But with that, we have to learn a couple of more stuff
05:35that will tie everything in together.
05:37So that's also one of the reasons
05:38why we're not digging in too deep into it early on.
05:41Now, I just want to show the other way
05:42of taking things out.
05:43We saw how to shift things out,
05:44which was taking it from the last,
05:47from taking Bill out from that first position
05:50and then moving him in to be the last thing.
05:52What I want to do is next is I want to move on
05:54and see like, let's see, how do we take the last thing?
05:56How do we take what we just pushed back in
05:59and unshift it out, right?
06:00But not unshift, we want to take it out.
06:02We want to, let's try to find it out, you know?
06:04And I don't remember it by heart,
06:06but I know it's inside here.
06:07So even just by surging for it,
06:11I'll probably find it, right?
06:12So it's not sore.
06:14There's a lot of different things
06:15you could do inside of an array.
06:18So let's just go through this pop.
06:19Here we go, pop.
06:21So pop is the opposite of push.
06:23And we find our found our pop.
06:25So we'll just call this popped.
06:29And if we run this one more time,
06:30we're going to see that our Bill should have been removed.
06:33And we could have done the exact same thing
06:34as we did before.
06:36Maybe we want to put Bill right back where he was before.
06:38To do that, all we have left to do
06:40is maybe ask our unshift again
06:44and put our popped right back where it was originally.
06:49So all this work was to take Bill,
06:51move him out, put him as last, move him out,
06:53put him back to be first.
06:55We're back to where we were.
06:57We're back to where we were in line 18,
06:59but after doing all this good stuff.
07:02Literally everything I want to talk about arrays right now,
07:05there's a lot more you could do with arrays.
07:07I encourage you to explore it.
07:09Obviously I encourage you to join zerothiggy.com.
07:11Sorry for the inner promotion,
07:13but really we have so much content in there
07:15that's beyond just JavaScript
07:16or just learning the core foundations of programming.
07:19If you feel like you need more of it,
07:20I truly and ultimately encourage you to check us out.
07:24All right, that's it about arrays,
07:26and we're moving on to our next topic.
