Lesson 22
Adding / Removing Elements at the End of JavaScript Array (push and pop)
Summary
# Summary of JavaScript Arrays Lesson
In this lesson from NDK Tech World, we continue our study of JavaScript arrays. Here are the key points covered:
## Accessing Array Elements
- An array, such as `names`, allows access to its elements using bracket notation with zero-based indexing.
## Modifying Arrays
- We learned to modify arrays using two methods: `push` and `pop`.
### Push
- The `push` method is used to add new elements to the **end** of the array.
- Example: `names.push('new name')` adds 'new name' to the end.
- The return value is the new length of the array.
### Pop
- The `pop` method removes the last element of the array.
- Example: `names.pop()` removes the last element and returns it.
- After calling `pop`, inspecting the array confirms that the last element has been removed.
## Conclusion
- Both `push` and `pop` affect the **end** of the array, either adding or removing elements. Keep this in mind when working with arrays in JavaScript.
Thank you for watching, and see you in the next lesson!
Video Transcript
Welcome back to NDK Tech World. Let's continue our study of JavaScript arrays.
So let me clear the console. I'm using Google Chrome this time. So we learned how
to access elements in an array, right? We had the names array with three elements
and to get each element you just use the brackets where brackets in the index,
right? Get John, get Ann and get Andrew. So the index always starts from zero. Now
how can we modify this array? That's what we're going to learn in this lesson.
We're going to talk about push and about pop. So push and pop, these are the
methods we can use to change the array, either to insert an item or to remove an
item. Now push and pop, as we're going to see, they add, you add either add to the
end of the array or if you remove from the end. So it's a little bit of constraint
there. So let's get started. Suppose I have this names and I want to add a new
name. So I can say names and I put a dot push. This is the method, okay? It's a
method called push. Then you put two parentheses because it's a function call.
And then you need to tell it, okay, what do you want to push? It could be multiple
things, but let's start like starts simple and just push one name. So you can push
another person. I don't know what to call the name or just say a new name. Let's see
what happens. So I say names dot push a new name. And I got this four back. The
return value here, this is called the return value of the result of this
expression. So this is just telling you how many elements are now present in the
array names. If you inspect names like so, you're going to notice that not only
John and an Android there, but also a new name has been added to the end of the
list. So that's what push does. It adds a new element at the end of the list. Okay.
Now we have four elements. Now let's learn how to remove an element from the end of
the array. That's called a pop operation. So if I say names dot pop, remember the
parentheses, because it's a function call. In this case, I don't need any arguments.
I just want this list called names to pop the last element. So if I say that, I
actually get as a return value from this function call, the element that was popped.
But if you inspect names now, you can see that new name is no longer there. So it
has been removed from the end of the array and was given as a return value of the
pop operation. Okay, so that's push and pop. Let's review what we did. So we have
an array called names that holds people's names. This case, we start off with three
names. Then we wanted to add a new name called new name to the end of the list. We
use the push operation, it always pushes to the end. Okay. And then we learn how to
remove the last element in the array, we're using the pop operation. So push and pop always
operate at the end of the array. Okay, keep that in mind. So I think we're good for this
video and until the next time, see you.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: