02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

unshift

Adding data to an array. now that we know how to push we need to know how to do it from the other end.

Video Transcript

00:00The next thing I want to do is see what it would be if we didn't want to push.
00:04What if we wanted to add a user to the top of the list?
00:07As we said, a raise or an index, and there's a priority in their order.
00:11And Cyrus is, for example, my boss.
00:13And thus, Cyrus is supposed to come first.
00:16Well, Sarah is not a boss.
00:18She's just a more junior employee.
00:20And for some reason, we want to sort our users not only by their membership level,
00:24but also by their seniority.
00:26So now, because Cyrus joined in first, let's just delete early.
00:30And let's just comment this out, right?
00:32We're just going to comment this out.
00:33And let's just add Sarah again to the end of the list.
00:38A users dot push.
00:40And we've seen that already.
00:41So we're going to send Sarah to the end of the list, as we've seen before.
00:44And now, if we want to put Cyrus to be the first in the list,
00:48the way we would do that in Flash is called unshift.
00:51And we're going to see in a minute what shift means.
00:53But if we want to call unshift, a users dot unshift,
00:57an unshift again could accept multiple parameters.
01:00But in arson hour, we're just going to use one, which will be Cyrus.
01:03And look what's going to happen here.
01:06First of all, our original array we're going to output,
01:08which is basically what you see right here.
01:10In the first line, we're adding to the end of the list.
01:13We're adding Sarah to the end of the list by pushing her to the end.
01:16And then we're unshifting Cyrus to the beginning of the array.
01:20Thus, we should see Cyrus in the beginning of the array.
01:22And Sarah should be in the end of the array.
01:24So if we run our application one more time now,
01:27we should now see that Cyrus is in the beginning of the list
01:30because we used unshift.
01:32And Sarah is in the end of the list.
01:34Now, let me just ask you a hypothetical question.
01:36What would happen if I would take this item and move it up?
01:39Would it matter?
01:40And the answer is right now, no, it wouldn't,
01:42because we're first pushing this to the beginning,
01:44and then we're pushing this item to the end.
01:46Although if we would push and then push again,
01:50there would definitely be a difference.
01:51Or if we would unshift and then unshift again,
01:53there would be a big difference.
01:55Because right now, this would be pushed to the first.
01:57But if we unshift again, let's do that.
02:00If I unshift, and let's say we have another manager,
02:03and his name is Bart.
02:04And we unshift Bart.
02:07Now, Bart will be first.
02:09So Cyrus was first in line 13.
02:11But as soon as we finish line 13, line 14 already
02:15is pushing Bart to be first.
02:16So if we run our application one more time again,
02:18we're going to see that Bart now is first
02:20and Cyrus is second.
02:21And same is true with pushing more items
02:24to the end of the list.
02:25So if I push, let's say, if we push Marge
02:31to the end of the list, then we'll
02:33see that Marge will be in the end of the list after Sarah.
02:36And it's very important not to forget this
02:38because you might find yourself in some messy bugs.
02:42If you don't realize the difference between unshift
02:44and push and making sure that you
02:46know the importance of the priority.
02:48So now if I change the order now, obviously,
02:50Marge will be first.
02:51And only after that, Sarah would come.
02:54So these are really important.
02:56And it goes back to these basic foundations
02:59of arrays, which is our last complex object
03:01that we're learning about in the basic,
03:04native, getting started learning programming.
03:08So this is very exciting.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!