Array Methods You Must Know

In the last article in the series we learnt what Arrays are in JavaScript why we need them and how we can create , update and iterate them.
In this article we will learn about some commonly used and very useful methods that come attached with an Array in JavaScript.
Array methods
Array methods or functions are part of a set of properties of any Array() you create in JavaScript. These methods gives you control and ability over any particular array to access, manipulate, transform, update and make other modifications on it.
Let's one by one see what these methods or functions are and how to use them
push() and pop()
Both of these methods work at the very end of an Array on which they are called, we will see in moment how, these methods adds and deletes an element from the end of an array respectively.
push() method:
This method takes a single or more parameters or values that need to be added into the array.
It takes one or more values.
Adds the passed values into the array
return new length of the array after adding the element into the array.
pop() method:
This method deletes the last element from the array and returns it upon being called on any particular array.
It does not take any value as input
Removes the last element and returns it
If the array was already empty it returns
undefined
shift() and unshift()
These methods remove and add elements from beginning of any array respectively.
shift() method:
This works on the beginning of the array and removes the first elements, you can also think of it like this method shifts the array elements to the left without preserving the first element.
Does not take any input
Removes left most or first element and returns it.
If array is empty it returns
undefined
unshift() method:
This method is exact same as push() method but instead of in the end it adds elements to the beginning of the array.
Takes one or more values to be added into the array.
Returns the new length after adding the new items.
map() method
This method takes a method or function as a parameter and changes or updates every element and makes a new array with those updated values, it does not change the original array or any of it's elements.
Takes a function as an input.
Changes or modifies every value based on the passed function
Returns new array with the new values
filter() method
This method also returns a new array , this method takes a method and if the element passed in the method results in a truthy ( true ) value, then this method adds the element in the new array or it skips it.
Takes a function as an input.
Adds it to a new array if given condition is true
returns the newly created array.
reduce() method
This method takes a function too , it takes two parameters, first one is a function and second one is an optional value called initial value.
The first parameter function, further takes two parameters and.. first one is a value that was returned by previous call of the method, second is current element of the array.
reduce() method returns the final value of the first parameter passed to the method that was itself passed as first parameter inside reduce().
See and example:
forEach() method
This method also takes a function and passes every element of the array in that function one by one.
forEach() does not return anything
Conclusion
These are the methods that are most commonly used with arrays inside JavaScript and most of the work that you need to do in a typical JavaScript program can be done using these methods or functions.






