15 Must-Know Javascript array methods
Learn about JavaScript Array Methods in simple way

I am a Frontend Web developer & a UI/UX Designer from India and I completed my graduation in Computer Science.
I am well-versed in Java, C, C++, Python, JavaScript, HTML, CSS, ReactJs, NodeJS, MySQL, and MongoDB.
I am eager to learn new technologies/frameworks and I strive hard in giving my 100% effort into building something new. In the future, I see myself as a successful Software Engineer. I want to teach and mentor students about coding and new technologies.
Arrays are an essential part of any programming language, and JavaScript is no exception. With arrays, developers can store and manipulate collections of data, including strings, numbers, and even objects. In this article, we will cover 25 must-know JavaScript array methods that everyone should know.
push()– Adds one or more elements to the end of an array Thepush()method adds one or more elements to the end of an array and returns the new length of the array. This method is useful when you need to add elements to an array without specifying the index.const array = [1, 2, 3]; array.push(4, 5); console.log(array); // Output: [1, 2, 3, 4, 5]pop()– Removes and returns the last element from an array Thepop()method removes and returns the last element from an array. This method is useful when you need to remove an element from the end of an array.
const array = [1, 2, 3];
const lastElement = array.pop();
console.log(array); // Output: [1, 2]
console.log(lastElement); // Output: 3
shift()– Removes and returns the first element from an array Theshift()method removes and returns the first element from an array. This method is useful when you need to remove an element from the beginning of an array.
const array = [1, 2, 3];
const firstElement = array.shift();
console.log(array); // Output: [2, 3]
console.log(firstElement); // Output: 1
unshift()– Adds one or more elements to the beginning of an array Theunshift()method adds one or more elements to the beginning of an array and returns the new length of the array. This method is useful when you need to add elements to an array at the beginning.
const array = [1, 2, 3];
array.unshift(4, 5);
console.log(array); // Output: [4, 5, 1, 2, 3]
splice()– Adds or removes elements from an array at a specified index Thesplice()method adds or removes elements from an array at a specified index. This method can be used to add or remove elements from anywhere in the array.
const array = [1, 2, 3, 4];
array.splice(1, 2, 5, 6);
console.log(array); // Output: [1, 5, 6, 4]
slice()– Returns a copy of a portion of an array specified by its starting and ending indexes Theslice()method returns a copy of a portion of an array specified by its starting and ending indexes. This method can be used to create a new array that contains a subset of the original array.
cCopy codeconst array = [1, 2, 3, 4];
const newArray = array.slice(1, 3);
console.log(newArray); // Output: [2, 3]
concat()– Combines two or more arrays and returns a new array Theconcat()method combines two or more arrays and returns a new array. This method can be used to join arrays together without modifying the original arrays.
arduinoCopy codeconst array1 = [1, 2];
const array2 = [3, 4];
const newArray = array1.concat(array2);
console.log(newArray); // Output: [1, 2, 3, 4]
join()– Joins all elements of an array into a string Thejoin()method joins all elements of an array into a string, using a specified separator. This method can be used to create a string from an array.const array = [1, 2, 3]; const string = array.join("-"); console.log(string); // Output: "1-2-3"
indexOf()– Returns the index of the first occurrence of a specified element in an array TheindexOf()method returns the index of the first occurrence of a specified element in an array. If the element is not found, this method returns -1.const array = [1, 2, 3]; const index = array.indexOf(2); console.log(index); // Output: 1
lastIndexOf()– Returns the index of the last occurrence of a specified element in an array ThelastIndexOf()method returns the index of the last occurrence of a specified element in array. If the element is not found, this method returns -1.const array = [1, 2, 3, 2]; const index = array.lastIndexOf(2); console.log(index); // Output: 3
forEach()– Executes a provided function once for each element in an array TheforEach()method executes a provided function once for each element in an array. This method can be used to perform an operation on each element of an array.const array = [1, 2, 3]; array.forEach((element) => { console.log(element); }); // Output: // 1 // 2 // 3
map()– Creates a new array with the results of calling a provided function on every element in the array Themap()method creates a new array with the results of calling a provided function on every element in the array. This method can be used to create a new array based on the original array.const array = [1, 2, 3]; const newArray = array.map((element) => { return element * 2; }); console.log(newArray); // Output: [2, 4, 6]
filter()– Creates a new array with all elements that pass a test specified by a provided function Thefilter()method creates a new array with all elements that pass a test specified by a provided function. This method can be used to create a new array based on a condition.const array = [1, 2, 3]; const newArray = array.filter((element) => { return element > 1; }); console.log(newArray); // Output: [2, 3]
reduce()– Executes a provided function on each element of the array and returns a single value Thereduce()method executes a provided function on each element of the array and returns a single value. This method can be used to perform an operation on all elements of an array and return a single value.const array = [1, 2, 3]; const sum = array.reduce((accumulator, currentValue) => { return accumulator + currentValue; }, 0); console.log(sum); // Output: 6
sort() – Sorts the elements of an array in place The sort() method sorts the elements of an array in place. This method can be used to sort an array in ascending or descending order.
const array = [3, 2, 1];
array.sort();
console.log(array); // Output: [1, 2, 3]
In this article, we have covered 15 must-know JavaScript array methods that everyone should know. These methods are essential for working with arrays in JavaScript and can greatly simplify your code. By using these methods, you can perform a wide range of operations on arrays, such as adding, removing, sorting, and filtering elements. Whether you're a beginner or an experienced developer, mastering these array methods will make your life easier and your code more efficient.
Those were the 15 must-know javascript array methods for you. I really hoped that you enjoyed reading this article.
Follow me on socials:
Twitter: https://twitter.com/Hy_piyush
Instagram: https://www.instagram.com/piyush_kesarwani22/
Linkedin: https://www.linkedin.com/in/piyush-kesarwani-809a30219/
GitHub: https://github.com/piyushkesarwani




