Introduction to JavaScript Array Methods
1 . What Are JavaScript Array Methods?
2 . The push() and pop() Methods
3 . The shift() and unshift() Methods
4 . Iterating with forEach()
5 . Transform Arrays Using map()
6 . Filter Elements with filter()
7 . Reduce Arrays with reduce()
8 . Find Elements with find() and findIndex()
9 . Check Conditions with some() and every()
10 . Combining Arrays with concat()
11 . Flatten Arrays Using flat()
12 . Sorting Arrays with sort()
14 . Slice vs Splice
15 . Convert Arrays to Strings
16 . Using includes() to Check Existence
17 . Copy Arrays with slice() and Spread Operator
18 . Advanced Iteration with entries(), keys(), values()
19 . Chaining Array Methods
20 . Best Practices with JavaScript Array Methods
Arrays are one of the most important data structures in programming, and mastering them in JavaScript is essential for every developer. JavaScript Array Methods provide tools to manipulate arrays efficiently, making your code cleaner, faster, and easier to maintain.
By the end of this guide, you'll learn how to:
We’ll break it down into subtopics, each with clear explanations and code examples.
Definition: Functions built into arrays that help manipulate, transform, or retrieve data.
Example: push, pop, map, filter, reduce
Arrays are objects with additional methods in JavaScript.
let fruits = ["apple", "banana"];
fruits.push("orange"); // adds item to array
console.log(fruits); // ["apple", "banana", "orange"]
Learn more about JavaScript arrays on MDN.
let numbers = [1, 2, 3];
numbers.push(4); // [1, 2, 3, 4]
numbers.pop(); // [1, 2, 3]
shift() removes the first element.
unshift() adds an element at the start.
Great for queue operations.
let letters = ["a", "b", "c"];
letters.shift(); // ["b", "c"]
letters.unshift("z"); // ["z", "b", "c"]
Executes a callback for every array element.
Does not return a new array.
let nums = [1, 2, 3];
nums.forEach((num) => console.log(num * 2));
See our JavaScript blog for more iteration tips.
let nums = [1, 2, 3];
let squared = nums.map(n => n * n);
console.log(squared); // [1, 4, 9]
let numbers = [1, 2, 3, 4, 5];
let even = numbers.filter(n => n % 2 === 0);
console.log(even); // [2, 4]
Combines array elements into a single value.
Useful for sums, counts, and accumulations.
let nums = [1, 2, 3, 4];
let total = nums.reduce((acc, curr) => acc + curr, 0);
console.log(total); // 10
let users = [{id:1}, {id:2}, {id:3}];
let user = users.find(u => u.id === 2);
console.log(user); // {id:2}
let nums = [2, 4, 6];
console.log(nums.every(n => n % 2 === 0)); // true
console.log(nums.some(n => n > 5)); // true
let a = [1,2];
let b = [3,4];
let combined = a.concat(b);
console.log(combined); // [1,2,3,4]
let nested = [1, [2, [3, 4]]];
console.log(nested.flat(2)); // [1,2,3,4]
let nums = [10, 2, 33];
nums.sort((a,b) => a-b);
console.log(nums); // [2, 10, 33]
13 . Reverse an Array with reverse()
let arr = [1,2,3];
arr.reverse();
console.log(arr); // [3,2,1]
slice(start, end) returns a new array without modifying the original.
splice(start, deleteCount, ...items) modifies the original array.
let arr = [1,2,3,4];
console.log(arr.slice(1,3)); // [2,3]
arr.splice(1,2); // [2,3] removed, arr = [1,4]
join() creates a string with a separator.
toString() converts array to a comma-separated string.
let arr = ['JS','HTML','CSS'];
console.log(arr.join(' - ')); // "JS - HTML - CSS"
let fruits = ['apple','banana'];
console.log(fruits.includes('apple')); // true
let arr = [1,2,3];
let copy1 = arr.slice();
let copy2 = [...arr];
console.log(copy1, copy2); // [1,2,3] [1,2,3]
let arr = ['a','b','c'];
for (let [i,v] of arr.entries()) {
console.log(i,v);
}
let nums = [1,2,3,4,5];
let result = nums.filter(n => n%2===0).map(n => n*10);
console.log(result); // [20,40]
let squares = [1,2,3].map(n => n*n);
Check our JavaScript blog for more array tips.
Software Engineer
Senior Software Engineer