Array Methods You Must Know

🧑💻 Software Developer @ TransUnion ⏳ 4+ Years Experience 🧠 Unsorted Array 🚀 Scalable Web Apps 🎥 Tech Educator in Progress
Search for a command to run...

🧑💻 Software Developer @ TransUnion ⏳ 4+ Years Experience 🧠 Unsorted Array 🚀 Scalable Web Apps 🎥 Tech Educator in Progress
No comments yet. Be the first to comment.
Why Do We Need Functions? When writing JavaScript, you’ll often repeat the same logic multiple times. For example, imagine you frequently need to add two numbers. Without functions, you might write so

Why Do Developers Use Object-Oriented Programming? When you start writing small JavaScript programs, you often store data in variables or simple objects. But as your applications grow, you begin to no

What Objects Are and Why They Are Needed? In JavaScript, objects are used to store related information together in a structured way. If you think about real-world things, most of them have multiple pi

What Arrays Are and Why We Need Them? When writing programs, we often need to store multiple related values. Imagine keeping a list of your favorite movies, a list of marks, or a list of daily tasks.

What Variables Are and Why They Are Needed? When writing a program, we often need a place to store information. This information could be a person’s name, age, score, or whether someone is logged in.

If you’re learning JavaScript, arrays are everywhere. Lists of numbers, names, products, messages — most real data lives inside arrays. Knowing how to work with arrays properly will make your code shorter, cleaner, and easier to understand.
Let’s walk through the array methods you’ll use almost daily.
For all examples, open your browser console and try them yourself.
These two methods work at the end of an array.
push() adds a new item to the end.pop() removes the last item.
let numbers = [10, 20, 30];
Before push:
[10, 20, 30]
numbers.push(40);
After push:
[10, 20, 30, 40]
Now removing the last item:
numbers.pop();
After pop:
[10, 20, 30]
Think of this like stacking plates. You add and remove from the top.
These methods work at the beginning of an array.
unshift() adds an item to the front.
shift() removes the first item.
let numbers = [10, 20, 30];
Before unshift:
[10, 20, 30]
numbers.unshift(5);
After unshift:
[5, 10, 20, 30]
Now remove the first item:
numbers.shift();
After shift:
[10, 20, 30]
If push/pop are like stacking plates, shift/unshift are like adjusting the front of a queue.
map() creates a new array by transforming every element.
Let’s double each number.
let numbers = [5, 10, 15];
Using a traditional loop:
let doubled = [];
for (let i = 0; i < numbers.length; i++) {
doubled.push(numbers[i] * 2);
}
Using map():
let doubled = numbers.map(function(num) {
return num * 2;
});
Before:
[5, 10, 15]
After:
[10, 20, 30]
map() is cleaner because it focuses on what you want. transforming each value, not on managing indexes.
filter() creates a new array with only the elements that match a condition.
Let’s keep numbers greater than 10.
let numbers = [5, 12, 8, 20];
Using a loop:
let result = [];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > 10) {
result.push(numbers[i]);
}
}
Using filter():
let result = numbers.filter(function(num) {
return num > 10;
});
Before:
[5, 12, 8, 20]
After:
[12, 20]
filter() reads more naturally: “Give me numbers greater than 10.”
reduce() combines all values into a single result.
A common use case is calculating the total sum.
let numbers = [5, 10, 15];
Using a loop:
let total = 0;
for (let i = 0; i < numbers.length; i++) {
total += numbers[i];
}
Using reduce():
let total = numbers.reduce(function(sum, num) {
return sum + num;
}, 0);
Result:
30
Think of reduce as a rolling calculator. It starts with an initial value (here 0) and keeps combining elements until only one value remains.
forEach() simply runs a function for each element in the array. It does not return a new array.
let numbers = [5, 10, 15];
numbers.forEach(function(num) {
console.log(num);
});
This prints:
5
10
15
Use forEach() when you want to perform an action, like logging or updating something, but not create a new array.