Loading
Lesson 23
Courses / JavaScript for Complete Beginners
Adding / Removing Elements at the Beginning of JavaScript Array (unshift and shift)

Video Transcript

Come back to NBKE Tech World. This lesson we will continue our study of JavaScript arrays. So we learn how to define an array, how to access individual elements, how to push and pop at the end of the array. Now let's learn how to do add and remove from the beginning of the array. We're going to use Unshift and Shift. Okay, so with the same array as before, oops, names, let me define it again. So we have an array of three elements. Now let's remove, let's actually add an item to the beginning of the array. So we can say names dot Unshift, this is the method that we'll add an element to the beginning of this list. Okay, so Unshift, whatever you want to add at the beginning, let's Unshift another name, for example. Now the return value is how many elements the array names are now have. So if you look at names, now we have another name at the beginning of the list. Okay. And I'll just keep in mind that the operations we did like push, pop, and even on Shift here, it modifies the original array. It does not create a new one. Be aware of that. Okay, that's why we now see there's another name inside names. Now, if you want to remove the first element of the list, you're going to do an inverse operation. And it's simply called shift without the on. So names dot shift, call this method, it will remove this first element from the array. Notice this expression will return the remove element. So you got the remove element, but the actual variable names will have been modified. Notice there's no longer a first element called another name. Now there's back to three elements. Okay, that's the length of the array. And that is something I haven't told you yet. If you want to find out how many elements there are inside a list, well, in this case, you can obviously tell right, John and Andrew, we have three. But internally, as you're creating and programming, you have your own program, your script, there's you cannot help the computer see there are three elements. So you have to explicitly tell or ask for the length of the array. And that's very easy to do. You just reference the array name, the variable name dot length, just like that. And that will give you the number elements in this array. Notice there's no two parentheses is just dot length. Okay, that's the property of this array. There are three elements. So let's review what we did. So we learned yet another set of operations on arrays. We learned how to add and remove add or remove an item at the beginning of the list. To add something to the beginning of the list, we use the on shift method. On shift returns the number elements, in the new in the array that was modified. We learned also how to add an element at the beginning of the list. That's how that's when we use shift, there's no on. And this operation, this statement, will return the removed element. But the original element, the original element, will return the element. And the original element, the original element, will return the element. And the this operation, this statement will return the removed element. But the original is changed permanently. Okay, keep that in mind. These operations change the original thing you're operating on. Then finally, when it up, just mentioning that you can get the array length, simply by saying dot length on the variable here. Okay, so that's it for now. Hope you liked the video and until the next time.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: