Array Methods You Must Know

Search for a command to run...

No comments yet. Be the first to comment.
When writing JavaScript code, you often need to perform calculations, compare values, or combine conditions. To do these tasks, JavaScript uses something called operators. If you’ve ever done basic ma

When learning JavaScript, you’ll eventually encounter a keyword that can feel confusing at first: this. The value of this changes depending on how a function is called. JavaScript also provides specia

Functions are one of the most important concepts in JavaScript. They help you organize your code and avoid repeating the same logic again and again. In JavaScript, there are multiple ways to create fu

When writing JavaScript programs, you often need to store multiple values together. For example, imagine you want to store a list of fruits: Apple Mango Banana Orange You could store them like t

As JavaScript applications grow, managing code and data can become complicated. To make programs easier to organize and reuse, developers often use a programming style called Object-Oriented Programmi

ygshjm
24 posts
Think of push() as a quick way to add something to the very end of your list. It doesn’t matter if it’s a number or a string like "yes"—it just goes to the last index.
Example: You have a small shopping list, and you remember one more thing.
State Before: [1, 2, 3]
let arr = [1, 2, 3];
arr.push(4); // Adding a number
arr.push("yes"); // Adding a string
[1, 2, 3, 4, "yes"]This is the opposite of push. It just grabs the very last item and kicks it out of the array.
["apple", "banana", "cherry"]let fruits = ["apple", "banana", "cherry"];
fruits.pop();
["apple", "banana"]While push and pop work at the back, these two work at the start of the array.
unshift(): Pushes a new item to the front (index 0).
shift(): Removes the very first item.
let queue = ["item1", "item2"];
queue.unshift("VIP"); // Now: ["VIP", "item1", "item2"]
queue.shift(); // Now: ["item1", "item2"]
Instead of writing a complex for loop to look at every item, forEach just "visits" every element and lets you do something with it.
let names = ["alice", "bob"];
names.forEach((name) => {
console.log("hello " + name);
});
This is where the magic happens. map() takes your array, changes every item based on your rule, and gives you a brand new array.
The Old Way (For Loop):
let nums = [1, 2, 3];
let doubled = [];
for(let i=0; i < nums.length; i++) {
doubled.push(nums[i] * 2);
}
The New Way (map):
let nums = [1, 2, 3];
let doubled = nums.map(n => n * 2);
State Before: [1, 2, 3]
State After: [2, 4, 6]
filter() looks at your items and only keeps the ones that follow your rule. It’s like a bouncer at a club.
The Old Way (For Loop):
let scores = [5, 12, 8, 20];
let highScores = [];
for(let i=0; i < scores.length; i++) {
if(scores[i] > 10) highScores.push(scores[i]);
}
The New Way (filter):
let scores = [5, 12, 8, 20];
let highScores = scores.filter(s => s > 10);
State Before: [5, 12, 8, 20]
State After: [12, 20]
Imagine you have a piggy bank. reduce() goes through your array, takes each number, and adds it into the piggy bank until you have one final total.
The Accumulator: Your piggy bank.
The Current: The coin you are currently holding.
let wallet = [10, 20, 30];
let total = wallet.reduce((piggyBank, coin) => {
return piggyBank + coin;
}, 0); // 0 is the starting amount in the piggy bank