Table of Contents
What push() Does
Adds items to the end: one or many, in a single call.
Changes the original array: it edits in place.
Returns the new length: not the array itself. This surprises a lot of people.
let tasks = ["study", "gym"];
let newLength = tasks.push("sleep");
console.log(tasks); // ["study", "gym", "sleep"]
console.log(newLength); // 3
Add several at once:
tasks.push("cook", "read");
console.log(tasks); // ["study", "gym", "sleep", "cook", "read"]
What pop() Does
Removes the last item: and gives it back to you.
Changes the original array: the array gets one item shorter.
Returns the removed item: handy when you want to use it.
Takes no arguments: you just call it.
let fruits = ["apple", "banana", "mango"]; let removed = fruits.pop(); console.log(removed); // "mango" console.log(fruits); // ["apple", "banana"]
console.log([].pop()); // undefined
Syntax
array.push(item1, item2, ...); array.pop();
push(): takes one or more items and returns the new length.
pop(): takes nothing and returns the removed item.
Practical Examples
let evens = [];
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
evens.push(i);
}
}
console.log(evens); // [2, 4, 6, 8, 10]
Undo history: pop the last action off.
let actions = ["type", "bold", "delete"]; let undone = actions.pop(); console.log(undone); // "delete" console.log(actions); // ["type", "bold"]Adding objects to a list:
let users = [];
users.push({ name: "Vi", age: 22 });
users.push({ name: "Sam", age: 30 });
console.log(users.length); // 2
Reversing a string with a stack:
let text = "hello";
let stack = [];
for (let char of text) {
stack.push(char);
}
let reversed = "";
while (stack.length > 0) {
reversed += stack.pop();
}
console.log(reversed); // "olleh"
Using Them Together as a Stack
Last in, first out: the last item you push is the first one you pop.
let stack = [];
stack.push("a");
stack.push("b");
stack.push("c");
console.log(stack.pop()); // "c"
console.log(stack.pop()); // "b"
console.log(stack); // ["a"]
Where it's used: undo features, the browser's back button, checking matching brackets, and function call tracking.Things to Remember
Both change the original array: copy it first if you need to keep the original.let original = [1, 2, 3]; let copy = [...original]; copy.push(4); console.log(original); // [1, 2, 3] console.log(copy); // [1, 2, 3, 4]push() with an array adds it as one item: it doesn't merge the items in.
let a = [1, 2]; a.push([3, 4]); console.log(a); // [1, 2, [3, 4]] let b = [1, 2]; b.push(...[3, 4]); console.log(b); // [1, 2, 3, 4]They're fast: working at the end of an array doesn't force other items to move, unlike shift() and unshift().
Common Mistakes
Expecting push() to return the array: it returns the new length, so you can't chain it.let result = [1, 2].push(3); console.log(result); // 3, not an array
Pushing an array when you meant to merge: use the spread operator or concat() instead.
Forgetting that they change the original: this causes bugs when the same array is used elsewhere.
Conclusion
Frequently Asked Questions (FAQs)
2.What happens if I call pop() on an empty array?The new length of the array, not the array itself.
3. How do I add or remove from the start of an array?It returns undefined and doesn't throw an error.
Use unshift() to add and shift() to remove.
0 Comments