Understanding JavaScript Array Methods and How to Use Them

Arrays are one of the most commonly used data structures in JavaScript. They allow us to store multiple values in a single variable. However, what makes arrays powerful are the built-in methods that let us manipulate, transform, and search through them easily.
In this article, we’ll go through some of the most important array methods, explain what they do, and show you how they are used in code.
1. push()
The push()
method adds one or more elements to the end of an array.
let numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers);
// Output: [1, 2, 3, 4]
2. pop()
The pop()
method removes the last element from an array and returns it.
let fruits = ["apple", "banana", "orange"];
let lastFruit = fruits.pop();
console.log(lastFruit);
// Output: "orange"
console.log(fruits);
// Output: ["apple", "banana"]
3. shift()
The shift()
method removes the first element of an array and returns it.
let names = ["John", "Mary", "James"];
let firstName = names.shift();
console.log(firstName);
// Output: "John"
console.log(names);
// Output: ["Mary", "James"]
4. unshift()
The unshift()
method adds one or more elements to the beginning of an array.
let colors = ["blue", "green"];
colors.unshift("red");
console.log(colors);
// Output: ["red", "blue", "green"]
5. map()
The map()
method creates a new array by applying a function to each element of the original array.
let numbers = [1, 2, 3, 4];
let doubled = numbers.map(num => num * 2);
console.log(doubled);
// Output: [2, 4, 6, 8]
6. filter()
The filter()
method returns a new array with elements that pass a certain condition.
let ages = [12, 18, 25, 30, 10];
let adults = ages.filter(age => age >= 18);
console.log(adults);
// Output: [18, 25, 30]
7. reduce()
The reduce()
method reduces an array to a single value by executing a function on each element.
let numbers = [1, 2, 3, 4];
let sum = numbers.reduce((total, num) => total + num, 0);
console.log(sum);
// Output: 10
8. forEach()
The forEach()
method executes a function for each element in the array.
let fruits = ["apple", "banana", "cherry"];
fruits.forEach(fruit => console.log(fruit));
// Output: apple
// banana
// cherry
9. find()
The find()
method returns the first element in the array that satisfies a condition.
let numbers = [5, 12, 8, 130];
let found = numbers.find(num => num > 10);
console.log(found);
// Output: 12
10. some()
The some()
method checks if at least one element in the array meets a condition.
let scores = [45, 50, 70, 30];
let passed = scores.some(score => score >= 50);
console.log(passed);
// Output: true
11. every()
The every()
method checks if all elements in the array meet a condition.
let ages = [18, 20, 25, 30];
let allAdults = ages.every(age => age >= 18);
console.log(allAdults);
// Output: true
12. includes()
The includes()
method checks if an array contains a certain value.
let colors = ["red", "blue", "green"];
console.log(colors.includes("blue"));
// Output: true
console.log(colors.includes("yellow"));
// Output: false
13. indexOf()
The indexOf()
method returns the first index of a value in the array, or -1 if it is not found.
let animals = ["cat", "dog", "rabbit"];
console.log(animals.indexOf("dog"));
// Output: 1
console.log(animals.indexOf("lion"));
// Output: -1
14. concat()
The concat()
method merges two or more arrays into a new array.
let arr1 = [1, 2];
let arr2 = [3, 4];
let combined = arr1.concat(arr2);
console.log(combined);
// Output: [1, 2, 3, 4]
15. slice()
The slice()
method returns a portion of an array without changing the original array.
let fruits = ["apple", "banana", "cherry", "date"];
let sliced = fruits.slice(1, 3);
console.log(sliced);
// Output: ["banana", "cherry"]
16. splice()
The splice()
method changes the array by adding, removing, or replacing elements.
let fruits = ["apple", "banana", "cherry"];
fruits.splice(1, 1, "orange");
console.log(fruits);
// Output: ["apple", "orange", "cherry"]
Conclusion
JavaScript provides a wide range of array methods that make it easier to manipulate data without writing long loops manually.
Use
push
,pop
,shift
, andunshift
for adding/removing items.Use
map
,filter
, andreduce
for transforming arrays.Use
find
,some
,every
, andincludes
for searching and checking.Use
slice
,splice
, andconcat
for restructuring arrays.
Mastering these methods will help you write cleaner, faster, and more efficient code in your projects.
Subscribe to my newsletter
Read articles from Aghagba Oghenemaro directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
