JavaScript Array Methods Every Developer Should Know π

Arrays are one of the most powerful data structures in JavaScript.
But what makes them truly powerful are the array methods that allow developers to manipulate, transform, and process data efficiently.
If you are a JavaScript developer, mastering array methods will make your code cleaner, more readable, and more functional.
Letβs explore some of the most useful JavaScript array methods.
1. push() β Add Element to the End
The push() method adds a new element to the end of an array.
const superheros = ["superman", "batman", "spiderman"];
superheros.push("wonderwoman");
console.log(superheros);
output:-
["superman", "batman", "spiderman", "wonderwoman"];
pop() β Remove Last Element
The pop() method removes the last element from an array.
const superheros = ["superman", "batman", "spiderman"];
superheros.pop();
console.log(superheros);
output:-
["superman", "batman"]
3. shift() β Remove First Element
shift() removes the first element of the array.
const superheros = ["superman", "batman", "spiderman"];
superheros.shift();
console.log(superheros);
output:-
["batman", "spiderman"]
4. unshift() β Add Element at the Beginning
unshift() adds an element to the start of an array.
const superheros = ["superman", "batman", "spiderman"];
superheros.unshift("wonderwoman");
console.log(superheros);
output:-
["wonderwoman", "superman", "batman", "spiderman"]
5. map() β Transform Data
The map() method creates a new array by transforming each element.
const numbers = [1,2,3,4];
const doubled = numbers.map(num => num * 2);
console.log(doubled);
output:-
[2,4,6,8]
6 . filter() β Select Specific Data
filter() returns elements that match a condition.
const numbers = [1,2,3,4,5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers);
output:-
[2,4]
7 . reduce() :-
reduce() processes all elements and returns a single value.
Example: Calculate sum of numbers:-
const numbers = [1,2,3,4];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum);
Explanation:-
accumulatorstarts with 0currentstarts with the first array element
Step-by-Step Visualization
Array:
[1, 2, 3, 4]
initial value:
accumulator = 0
Step 1
0 + 1 = 1
accumulator becomes 1
Step 2
1 + 2 = 3
accumulator becomes 3
Step 3
3 + 3 = 6
accumulator becomes 6
Step 4
6 + 4 = 10
Final result:
10
Visual Flow :-
Initial value
β
0
β
0 + 1 β 1
1 + 2 β 3
3 + 3 β 6
6 + 4 β 10
Final Output β 10;
8 . forEach():-
forEach() is used to Updating an Array Element.
// Grocery list
let items = ["Bread", "Milk", "Eggs", "Sugar", "Chocolates"];
console.log("Before update:", items);
// Update the second item in the array
items[1] = "Toothpaste";
console.log("After update:", items);
// Loop through the array
items.forEach((item, index) => {
console.log(index, item);
});
output:-
Before update: ["Bread","Milk","Eggs","Sugar","Chocolates"]
After update: ["Bread","Toothpaste","Eggs","Sugar","Chocolates"]
0 Bread
1 Toothpaste
2 Eggs
3 Sugar
4 Chocolates
======================= Make Coding Simple, Enjoy the process ================


