Some Most Important Array Method In Javascript

Arrays are a powerful and comprehensive tool of Javascript. They are very intuitive to use and you can do almost everything with them. Simply In JS Arrays can store multiple data(datatypes) in a single variable.

An array can be created by enclosed the items of it within square brackets. For this example, we’ll imagine that we are looking at a list of fruits.

let fruits = [“mango”, “apple”, “orange”];

where “mango”, “apple”, “orange” each of them are array elements and that means the length of the array is 3.

Methods of Array:

The strength of JavaScript arrays lies in the array methods which are the properties of the array. These methods are built-in to JavaScript functions that we can apply to arrays — Each method has a unique function that performs a change or calculation to the array and saves us from writing common functions from scratch.

There are several built-in methods to use in arrays, and we will go over a few of the most commonly used ones.

map()

The map method is used to create a new array from an existing one by applying a function to each of the elements of the first array. It does not change the original array.

filter()

The filter method applies the function that returns one new array with the entire set of elements that satisfies the condition inside the provided function.

forEach()

The forEach method is used to get all elements in array one by one without using a loop. A callback function is added as a parameter in forEach.

find()

The find method only returns the value of the first element that satisfies the condition inside the provided function.

reduce()

The reduce method executes a function against an accumulator and each of the array elements to reduce it to a single value.

sort()

The sort method simply allows us to arrange the elements of an array and returning the sorted array.

indexof()

The indexOf method returns the index of the first element in the array or -1 if it is not found. Remember in array index starts from 0.

push() ,pop(),shift() & unShift()

The push method used to add one or more elements at the end of the array and return a new array.

The pop method used to delete the last element from the array and return that deleted one.

Similarly, The shift method used to delete the first element from the array and return that deleted one.

The unShift method used to add one or more elements at the beginning of the array and return a new array.

Next, we will discuss about how to add or delete any element at any position and also some more interesting array methods.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store