The most commonly used array methods in JavaScript

Kavindu Samarasinghe
4 min readMay 7, 2023

The most commonly used array methods in JavaScript by Kavindu Akash

JavaScript arrays are a fundamental data structure that is used to store collections of data. Arrays can hold any type of data, including numbers, strings, objects, and even other arrays. They are also dynamic, which means they can be resized as needed.

In JavaScript, there are a variety of methods available for working with arrays. These methods can be used to add, remove, and modify elements in an array, as well as to perform operations on the entire array.

Let’s explore some of the most commonly used array methods in JavaScript😀

concat() - Joins two or more arrays and returns a new array.

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = arr1.concat(arr2);

console.log(arr3); // Output: [1, 2, 3, 4, 5, 6]

filter() - Creates a new array with all elements that pass the test implemented by the provided function.

const arr = [1, 2, 3, 4, 5];

const filteredArr = arr.filter(function(element) {
return element > 3;
});

console.log(filteredArr); // Output: [4, 5]

find() - Returns the value of the first element in the array that satisfies the provided testing function.

const arr = [1, 2, 3, 4, 5];

const foundElement = arr.find(function(element) {
return element > 3;
});

console.log(foundElement); // Output: 4

forEach() - Executes a provided function once for each array element.

const arr = [1, 2, 3];

arr.forEach(function(element) {
console.log(element);
});

// Output:
// 1
// 2
// 3

includes() - Determines whether an array includes a certain value among its entries.

const arr = [1, 2, 3];

console.log(arr.includes(2)); // Output: true
console.log(arr.includes(4)); // Output: false

indexOf() - Returns the first index at which a given element can be found in the array.

const arr = [1, 2, 3, 4, 5];

console.log(arr.indexOf(2)); // Output: 1
console.log(arr.indexOf(6)); // Output: -1

join() - Joins all elements of an array into a string.

const arr = [1, 2, 3];

const joinedString = arr.join('-');

console.log(joinedString); // Output: "1-2-3"

lastIndexOf() - Returns the last index at which a given element can be found in the array.

const arr = [1, 2, 3, 4, 5, 2];

console.log(arr.lastIndexOf(2)); // Output: 5

map() - Creates a new array with the results of calling a provided function on every element in the array.

const arr = [1, 2, 3];

const mappedArr = arr.map(function(element) {
return element * 2;
});

console.log(mappedArr); // Output: [2, 4, 6]

pop() - Removes the last element from an array and returns that element.

const arr1 = [1, 2, 3, 4];
const popped = arr1.pop(); // popped = 4, arr1 = [1, 2, 3]

push() - Adds one or more elements to the end of an array and returns the new length of the array.

const arr2 = [1, 2, 3];
const newLength = arr2.push(4, 5); // newLength = 5, arr2 = [1, 2, 3, 4, 5]

reduce() - Applies a function against an accumulator and each element in the array to reduce it to a single value.

const arr3 = [1, 2, 3, 4];
const sum = arr3.reduce((accumulator, currentValue) => accumulator + currentValue); // sum = 10

reverse() - Reverses the order of the elements in an array.

const arr4 = [1, 2, 3, 4];
arr4.reverse(); // arr4 = [4, 3, 2, 1]

shift() - Removes the first element from an array and returns that element.

const arr5 = [1, 2, 3];
const shifted = arr5.shift(); // shifted = 1, arr5 = [2, 3]

slice() - Returns a shallow copy of a portion of an array into a new array.

const arr6 = [1, 2, 3, 4];
const sliced = arr6.slice(1, 3); // sliced = [2, 3], arr6 = [1, 2, 3, 4]

some() - Tests whether at least one element in the array passes the test implemented by the provided function.

const arr7 = [1, 2, 3, 4];
const hasEven = arr7.some((num) => num % 2 === 0); // hasEven = true

sort() - Sorts the elements of an array in place and returns the array.

const arr8 = [4, 2, 3, 1];
arr8.sort(); // arr8 = [1, 2, 3, 4]

splice() - Adds or removes elements from an array at a specified position.

const arr9 = [1, 2, 3, 4];
const removed = arr9.splice(1, 2); // removed = [2, 3], arr9 = [1, 4]

toString() - Returns a string representing the specified array and its elements.

const arr10 = [1, 2, 3];
const arrAsString = arr10.toString(); // arrAsString = "1,2,3"

unshift() - Adds one or more elements to the beginning of an array and returns the new length of the array.

const arr11 = [1, 2, 3];
const newLength2 = arr11.unshift(4, 5); // newLength2 = 5, arr11 = [4, 5, 1, 2, 3]

Thanks for reading 💙

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Kavindu Samarasinghe
Kavindu Samarasinghe

No responses yet

Write a response