Array Methods You Must Know In JavaScript

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
In this article we'll explore all must know array methods with easy explanations along with good examples. Before jumping on array methods lets clarify the understanding of array.
Arrays are zero-indexed ordered collection of values stored in a single variable. They are special type of objects in JavaScript. Arrays are passed by reference and not passed by value. Arrays in JavaScript are resizable unlike some other languages. In other words array elements can be added or removed and the length property automatically updates.
Array can be created using two ways in JavaScript:
const arr = [];
const arr = new Array()
Array methods are pre-built functions which perform specific tasks. It's availability make developers life easy, saves times and reduces bug for such tasks. So yeah... they are great help. So knowing how they work and which method performs what is a must know! Now we'll explore the important, most used array methods and will also consider which method mutates original array.
Push: add a new element at the end of the array and mutates the original array and return new length.
const arr = [1,2,3,4,5]
console.log(arr.push(6))// 6 - new length
console.log(arr) // [1,2,3,4,5,6]
pop: remove last element of the array returns removed element and mutates original array.
const arr = [1,2,3,4,5]
console.log(arr.pop()) // 5 -> remove last element and return it
console.log(arr) // [1,2,3,4] -> change original
shift: remove element from the start, returns removed element and mutates original array.
const arr = [1,2,3,4,5]
console.log(arr.shift()) // 1 -> remove first element and return it
console.log(arr) // [2,3,4,5] -> change original
unshift: add element at the start , return new length and change original array.
const arr = [1,2,3,4,5]
console.log(arr.unshift(0)) // 6 -> return new length
console.log(arr) // [0,1,2,3,4,5] -> change original
map: map method accepts a function (callback) and perform the function on each element , it returns new array and does not mutates original array.
const arr = [1,2,3,4];
const result = arr.map((i)=> i*2)
console.log(result); // [2,4,6,8]
reduce: takes a callback and initializer( if initial value is not given then by default first element becomes initial value). This method performs the callback on each element and reduces the values to single value and return it. It does not mutates original array.
syntax:
array.reduce(callback, initial value)
const arr = [1,2,3,4,5]
const result = arr.reduce((pv,cv)=> pv + cv,0);
console.log(result) // 15
filter: accepts callback which performs the function on each element, returns new array of only those element which are true for callback. It does not mutates original array.
const arr = [10,20,30,40,50];
const result = arr.filter((e)=> e > 20);
console.log(result); // [30,40,50]
forEach; takes callback which performs on each element of the array. forEach does not return anything, it returns undefined therefore it can not be chained. forEach itself does not mutate the array, but you can mutate the array inside the callback.
const arr = [1, 2, 3];
// This will NOT mutate the array (primitives passed by value)
arr.forEach(function(element) {
element = element * 2; // Changes local copy only
});
console.log(arr); // Output: [1, 2, 3]
// This WILL mutate the array (explicit assignment using index)
arr.forEach(function(element, index, theArray) {
theArray[index] = element * 2; // Modifies the original array
});
console.log(arr); // Output: [2, 4, 6]
forEach() is ideal for performing an action on each item where you do not need to create a new array, such as:
Logging array elements to the console.
Updating the DOM (Document Object Model).
Modifying an external variable (side effects)
In this article we cover basic but important array methods which are a must know for any developer. You can only optimize things that you understand.. So now that you know these methods practice a lot in console.
Keep being happy keep coding!