Top 10 my favorite Array Methods in JavaScript. Simply Explained.

Table of contents

JavaScript arrays are one of the most powerful and widely used data structures. But I am not going to explain every methods present in the JavaScript language. Instead, I'll focus on the most important ones that you will use frequently. Let's dive into some key array methods that can help you in your coding journey.
- push() - Inserts an element
The push()
method adds a new element to the end of an array you have already declared. When you call this array, it returns the updated array with the new element added at the end, increasing the array's size.
- pop() - Remove an element
The pop() method removes the last element from an array. When you call the array, it returns the remaining elements in the array. And that basically it, You don’t need to care much.
- unshift() - Insert the new element at 0 index
The unshift() method inserts a new element at the 0 index of the array, increasing the array's length as well.
- sort() - sort the given elements
There are two types of the sort() method used in array methods. Typically, if you use this method, it will only compare the starting character of each element. Once it recognizes the pattern, it will arrange the elements accordingly in the array. Consider the following example:
Second sort() method for numbers
In this case, we need to use a function to sort whole numbers, not just the first character of each element. Consider the following example:
- filter() - Filter elements in the Array
The filter() method filters out array elements based on certain conditions. You should define these conditions after declaring the array, along with the method itself. Consider the following example:
- some() - Checking if elements Matches
The some() method checks if an element is present in the given array. This method is somewhat similar to the filter() method. Consider the following example:
- splice() - Modifying an Array
The splice() method adds or removes elements from an array. Consider the following example for removing an item from an array:
To add an element:
- map() - Operate on every single element in the array
The map() method of array instances creates a new array filled with the results of applying a given function to each element in the original array. Consider the following example:
- keys() - Extract index values
The keys() method of array instances returns a new array iterator object that contains the keys for each index in the array. Consider the following example:
- flat() - Add Sub-array with parent array
The flat() method of array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. Consider the following example:
Note:- A parent array may have multiple sub-arrays. In that case, you need to use a loop to return a flat array.
Conclusion:
In conclusion, JavaScript offers many more array methods, each with its own functionality that can make major or minor changes to your code. The use of these array methods can vary depending on the project you're working on or the specific operation you want to perform. I've included a link to the MDN documentation so you can explore other available methods for working with arrays in JavaScript.
Thank you so much for reading my blog. See you!
Subscribe to my newsletter
Read articles from Md Ehtesham directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
